第一種方法:
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;
}