js阿拉伯數字轉中文大寫

來源:互聯網
上載者:User

第一種方法:

 

function DX(n) { //金額大寫轉換函式
if
(!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
return "資料非法";
var unit =
"千百拾億千百拾萬千百拾元角分", str = "";
n += "00";
var p = n.indexOf('.');
if (p
>= 0)
n = n.substring(0, p) + n.substr(p+1, 2);
unit =
unit.substr(unit.length - n.length);
for (var i=0; i < n.length;
i++)
str += '零壹貳三肆伍陸柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
return
str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(萬|億|元)/g,
"$1").replace(/(億)萬|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g,
"元整");
}

 

第二種方法:

function i2c(n){
if(typeof(n) == "number"){
if(-1 ==
n.toString().indexOf(".")){
return seti2c(n);
}else{
var
i,istr,f,fstr,a,rstr;
a = n.toString().split(".");
i = a[0];
f =
a[1];
istr = seti2c(i);
fstr = setf2c(f);
rstr = istr+"零"+fstr;
rstr
= rstr.replace(/零+/g,"零");
return rstr;
}
}else{
return
"---";
}
}

function seti2c(n){
var ns = n.toString();
var tempstr = "";
for(var
i=1;i<ns.length+1;i++){
switch(i){
case 1:
var t =
ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"圓"+tempstr;
}else{
tempstr = "圓"+tempstr;
}
break;
case
2:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"拾"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
3:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"佰"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
4:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"仟"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
5:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"萬"+tempstr;
}else{
tempstr = "萬"+tempstr;
}
break;
case
6:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"拾"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
7:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"佰"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
8:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"仟"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
9:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"億"+tempstr;
}else{
tempstr = "億"+tempstr;
}
break;
case
10:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"拾"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
11:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"佰"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
12:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"仟"+tempstr;
}else{
tempstr =
"0"+tempstr;
}
break;
default:break;
}
}
return
g2b(tempstr);
}

function setf2c(n){
var ns = n.toString();
var tempstr = "";
for(var
i=0;i<ns.length;i++){
switch(i){
case 0:
var t =
ns.substr(i,1);
if(t!="0"){
tempstr += t+"角";
}else{
tempstr +=
"0";
}
break;
case 1:
var t =
ns.substr(i,1);
if(t!="0"){
tempstr +=
t+"分";
}
break;
default:break;
}
}
return
g2b(tempstr);
}

function g2b(s){
rs = s.replace(/0+/g,"0");
rs =
rs.replace("0圓","圓");
rs = rs.replace("0億","億");
rs =
rs.replace("0萬","萬");
rs = rs.replace("億萬","億");
rs =
rs.replace(/0/g,"零");
rs = rs.replace(/1/g,"壹");
rs =
rs.replace(/2/g,"貳");
rs = rs.replace(/3/g,"三");
rs =
rs.replace(/4/g,"肆");
rs = rs.replace(/5/g,"伍");
rs =
rs.replace(/6/g,"陸");
rs = rs.replace(/7/g,"柒");
rs =
rs.replace(/8/g,"捌");
rs = rs.replace(/9/g,"玖");
return rs;
}

第一種方法:

 

function DX(n) { //金額大寫轉換函式
if
(!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
return "資料非法";
var unit =
"千百拾億千百拾萬千百拾元角分", str = "";
n += "00";
var p = n.indexOf('.');
if (p
>= 0)
n = n.substring(0, p) + n.substr(p+1, 2);
unit =
unit.substr(unit.length - n.length);
for (var i=0; i < n.length;
i++)
str += '零壹貳三肆伍陸柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
return
str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(萬|億|元)/g,
"$1").replace(/(億)萬|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g,
"元整");
}

 

第二種方法:

function i2c(n){
if(typeof(n) == "number"){
if(-1 ==
n.toString().indexOf(".")){
return seti2c(n);
}else{
var
i,istr,f,fstr,a,rstr;
a = n.toString().split(".");
i = a[0];
f =
a[1];
istr = seti2c(i);
fstr = setf2c(f);
rstr = istr+"零"+fstr;
rstr
= rstr.replace(/零+/g,"零");
return rstr;
}
}else{
return
"---";
}
}

function seti2c(n){
var ns = n.toString();
var tempstr = "";
for(var
i=1;i<ns.length+1;i++){
switch(i){
case 1:
var t =
ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"圓"+tempstr;
}else{
tempstr = "圓"+tempstr;
}
break;
case
2:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"拾"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
3:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"佰"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
4:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"仟"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
5:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"萬"+tempstr;
}else{
tempstr = "萬"+tempstr;
}
break;
case
6:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"拾"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
7:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"佰"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
8:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"仟"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
9:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"億"+tempstr;
}else{
tempstr = "億"+tempstr;
}
break;
case
10:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"拾"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
11:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"佰"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case
12:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr =
t+"仟"+tempstr;
}else{
tempstr =
"0"+tempstr;
}
break;
default:break;
}
}
return
g2b(tempstr);
}

function setf2c(n){
var ns = n.toString();
var tempstr = "";
for(var
i=0;i<ns.length;i++){
switch(i){
case 0:
var t =
ns.substr(i,1);
if(t!="0"){
tempstr += t+"角";
}else{
tempstr +=
"0";
}
break;
case 1:
var t =
ns.substr(i,1);
if(t!="0"){
tempstr +=
t+"分";
}
break;
default:break;
}
}
return
g2b(tempstr);
}

function g2b(s){
rs = s.replace(/0+/g,"0");
rs =
rs.replace("0圓","圓");
rs = rs.replace("0億","億");
rs =
rs.replace("0萬","萬");
rs = rs.replace("億萬","億");
rs =
rs.replace(/0/g,"零");
rs = rs.replace(/1/g,"壹");
rs =
rs.replace(/2/g,"貳");
rs = rs.replace(/3/g,"三");
rs =
rs.replace(/4/g,"肆");
rs = rs.replace(/5/g,"伍");
rs =
rs.replace(/6/g,"陸");
rs = rs.replace(/7/g,"柒");
rs =
rs.replace(/8/g,"捌");
rs = rs.replace(/9/g,"玖");
return rs;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.