数字大写转换
程序员文章站
2022-04-09 21:41:56
...
还有一点bug没有处理:没有限制长度,没有处理以零开的头的数字
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<style>
</style>
</head>
<body>
<script type="text/javascript">
var num = window.prompt();
var a = '零壹贰叁肆伍陆柒捌玖';
var b = '个万亿';
var c = '个拾佰仟'
var len = num.length;
var flag = 0;
var ant = '';
for (var i = 0; i < len; i++) {
var k = (len - i) - Math.floor((len - i) / 4) * 4;
if (k == 0) k = 4;
k -= 1;
if (num[i] != 0) {
if(flag!=0){
flag=0;
ant+=a[0];
}
ant += a[num[i]];
if (k != 0)
ant += c[k];
}
else
flag++;
if (k==0&&Math.floor((len - i) / 4) != 0) {
ant += b[Math.floor((len - i) / 4)];
flag=0;
}
}
document.write(num+'</br>')
document.write('<h2>'+ant+'</h2>');
</script>
<div>
</div>
</body>
</html>
上一篇: 你不知道的console