<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script>
function numToCny(num) {
var str1 = '零壹贰叁肆伍陆柒捌玖'; //0-9所对应的汉字
var str2 = '万仟佰拾亿仟佰拾万仟佰拾元角分'; //数字位所对应的汉字
var str3; //从原num值中取出的值
var str4; //数字的字符串形式
var str5 = "; //人民币大写金额形式
var i; //循环变量
var j; //num的值乘以100的字符串长度
var ch1; //数字的汉语读法
var ch2; //数字位的汉字读法
var nzero = 0; //用来计算连续的零值是几个
num = Math.abs(num).toFixed(2); //将num取绝对值并四舍五入取2位小数
str4 = (num * 100).toFixed(0).toString(); //将num乘100并转换成字符串形式
j = str4.length; //找出最高位
if (j > 15) { return '溢出'; }
str2 = str2.substr(15 – j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分
//循环取出每一位需要转换的值
for (i = 0; i < j; i++) {
str3 = str4.substr(i, 1); //取出需转换的某一位的值
if (i != (j – 3) && i != (j – 7) && i != (j – 11) && i != (j – 15)) { //当所取位数不为元、万、亿、万亿上的数字时
if (str3 == '0') {
ch1 = ";
ch2 = ";
nzero = nzero + 1;
}
else {
if (str3 != '0' && nzero != 0) {
ch1 = '零' + str1.substr(str3 * 1, 1);
ch2 = str2.substr(i, 1);
nzero = 0;
}
else {
ch1 = str1.substr(str3 * 1, 1);
ch2 = str2.substr(i, 1);
nzero = 0;
}
}
}
else { //该位是万亿,亿,万,元位等关键位
if (str3 != '0' && nzero != 0) {
ch1 = "零" + str1.substr(str3 * 1, 1);
ch2 = str2.substr(i, 1);
nzero = 0;
}
else {
if (str3 != '0' && nzero == 0) {
ch1 = str1.substr(str3 * 1, 1);
ch2 = str2.substr(i, 1);
nzero = 0;
}
else {
if (str3 == '0' && nzero >= 3) {
ch1 = ";
ch2 = ";
nzero = nzero + 1;
}
else {
if (j >= 11) {
ch1 = ";
nzero = nzero + 1;
}
else {
ch1 = ";
ch2 = str2.substr(i, 1);
nzero = nzero + 1;
}
}
}
}
}
if (i == (j – 11) || i == (j – 3)) { //如果该位是亿位或元位,则必须写上
ch2 = str2.substr(i, 1);
}
str5 = str5 + ch1 + ch2;
if (i == j – 1 && str3 == '0') { //最后一位(分)为0时,加上“整”
str5 = str5 + '整';
}
}
if (num == 0) {
str5 = '零元整';
}
return str5;
}
function upDigit(n) {
var fraction = ['角', '分'];
var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
var unit = [['元', '万', '亿'], [", '拾', '佰', '仟']];
var head = n < 0 ? '欠' : ";
n = Math.abs(n);
var s = ";
for (var i = 0; i < fraction.length; i++) {
s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, ");
}
s = s || '整';
n = Math.floor(n);
for (var i = 0; i < unit[0].length && n > 0; i++) {
var p = ";
for (var j = 0; j < unit[1].length && n > 0; j++) {
p = digit[n % 10] + unit[1][j] + p;
n = Math.floor(n / 10);
}
s = p.replace(/(零.)*零$/, ").replace(/^$/, '零') + unit[0][i] + s;
}
return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
}
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, "元整");
}
var i = 100100;
alert(numToCny(i) + "\n" + upDigit(i) + "\n" + DX(i));
</script>
</head>
<body>
</body>
</html>