价格转换大写

<!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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值