<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
var Money = function(money) {
this.money = money;
this.strNum = '零壹贰叁肆伍陆柒捌玖'; // 对应中文数字
this.strUnit = '万仟佰拾亿仟佰拾万仟佰拾元角分'; // 对应单位
return this;
};
Money.prototype.format = function() {
var numberValue = new String(Math.round(this.money * 100)); // 数字金额
var chineseValue = ""; // 转换后的中文金额
var len = numberValue.length; // 输入数字的长度
var Ch1; // 数字的中文读法
var Ch2; // 数字位的中文读法
var nZero = 0; // 计算连续的零值的个数
var index; // 指定位置的数值
if (len > 15) {
alert('超出计算范围');
return ;
}
if (numberValue == 0) {
chineseValue == "零元整";
return chineseValue;
}
this.strUnit = this.strUnit.substr(this.strUnit.length - len, len); // 取出对应位数的单位的值
for (var i = 0; i < len; i ++) {
index = parseInt(numberValue.substr(i, 1), 10); // 取出需要转换的某一位的值
if (i != (len - 3) && i != (len - 7) && i != (len - 11) && i != (len - 15)) {
if (index == 0) {
Ch1 = "";
Ch2 = "";
nZero = nZero + 1;
} else if (index != 0 && nZero != 0) {
Ch1 = "零" + this.strNum.substr(index, 1);
Ch2 = this.strUnit.substr(i, 1);
nZero = 0
} else {
Ch1 = this.strNum.substr(index, 1);
Ch2 = this.strUnit.substr(i, 1);
nZero = 0;
}
} else {
if (index != 0 && nZero != 0) {
Ch1 = "零" + this.strNum.substr(index, 1);
Ch2 = this.strUnit.substr(i, 1);
nZero = 0;
} else if (index != 0 && nZero == 0) {
Ch1 = this.strNum.substr(index, 1);
Ch2 = this.strUnit.substr(i, 1);
nZero = 0
} else if (index == 0 && nZero >= 3) {
Ch1 = "";
Ch2 = "";
nZero = nZero + 1;
} else {
Ch1 = "";
Ch2 = this.strUnit.substr(i, 1);
nZero = nZero + 1;
}
if (i == (len - 11) || i == (len - 3)) {
Ch2 = this.strUnit.substr(i, 1)
}
}
chineseValue = chineseValue + Ch1 + Ch2;
}
if (index == 0) {
chineseValue = chineseValue + "整";
}
return chineseValue;
};
var vue = new Money(123456789);
console.log(vue.format())
</script>
</html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
var Money = function(money) {
this.money = money;
this.strNum = '零壹贰叁肆伍陆柒捌玖'; // 对应中文数字
this.strUnit = '万仟佰拾亿仟佰拾万仟佰拾元角分'; // 对应单位
return this;
};
Money.prototype.format = function() {
var numberValue = new String(Math.round(this.money * 100)); // 数字金额
var chineseValue = ""; // 转换后的中文金额
var len = numberValue.length; // 输入数字的长度
var Ch1; // 数字的中文读法
var Ch2; // 数字位的中文读法
var nZero = 0; // 计算连续的零值的个数
var index; // 指定位置的数值
if (len > 15) {
alert('超出计算范围');
return ;
}
if (numberValue == 0) {
chineseValue == "零元整";
return chineseValue;
}
this.strUnit = this.strUnit.substr(this.strUnit.length - len, len); // 取出对应位数的单位的值
for (var i = 0; i < len; i ++) {
index = parseInt(numberValue.substr(i, 1), 10); // 取出需要转换的某一位的值
if (i != (len - 3) && i != (len - 7) && i != (len - 11) && i != (len - 15)) {
if (index == 0) {
Ch1 = "";
Ch2 = "";
nZero = nZero + 1;
} else if (index != 0 && nZero != 0) {
Ch1 = "零" + this.strNum.substr(index, 1);
Ch2 = this.strUnit.substr(i, 1);
nZero = 0
} else {
Ch1 = this.strNum.substr(index, 1);
Ch2 = this.strUnit.substr(i, 1);
nZero = 0;
}
} else {
if (index != 0 && nZero != 0) {
Ch1 = "零" + this.strNum.substr(index, 1);
Ch2 = this.strUnit.substr(i, 1);
nZero = 0;
} else if (index != 0 && nZero == 0) {
Ch1 = this.strNum.substr(index, 1);
Ch2 = this.strUnit.substr(i, 1);
nZero = 0
} else if (index == 0 && nZero >= 3) {
Ch1 = "";
Ch2 = "";
nZero = nZero + 1;
} else {
Ch1 = "";
Ch2 = this.strUnit.substr(i, 1);
nZero = nZero + 1;
}
if (i == (len - 11) || i == (len - 3)) {
Ch2 = this.strUnit.substr(i, 1)
}
}
chineseValue = chineseValue + Ch1 + Ch2;
}
if (index == 0) {
chineseValue = chineseValue + "整";
}
return chineseValue;
};
var vue = new Money(123456789);
console.log(vue.format())
</script>
</html>