JS中怎么把一个数字变成大写的啊

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS中怎么把一个数字变成大写的啊相关的知识,希望对你有一定的参考价值。

将2345,变成贰叁肆伍,谢谢各位大神

参考技术A 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, "元整");
参考技术B

上代码!

        var num=2345;
var arr = new Array('林','呀','而','撒','斯','无');
num=num+'';
var result=new Array();

for (var i=0;i< num.length ;i++) 
result.push(arr[num.charAt(i)]);



alert(result.join(','))

本回答被提问者和网友采纳

js怎么把数字金额转换为大写金额

参考技术A 百度一下这种写好的js很多 随便选个就可以;我帮你搜了个;
function Arabia_to_Chinese(Num)
for(i=Num.length-1;i>=0;i--)

Num = Num.replace(",","")
Num = Num.replace(" ","")

Num = Num.replace("¥","")
if(isNaN(Num))
alert("请检查小写金额是否正确");
return;

part = String(Num).split(".");
newchar = "";
for(i=part[0].length-1;i>=0;i--)
if(part[0].length > 10) alert("位数过大,无法计算");return "";
tmpnewchar = ""
perchar = part[0].charAt(i);
switch(perchar)
case "0": tmpnewchar="零" + tmpnewchar ;break;
case "1": tmpnewchar="壹" + tmpnewchar ;break;
case "2": tmpnewchar="贰" + tmpnewchar ;break;
case "3": tmpnewchar="叁" + tmpnewchar ;break;
case "4": tmpnewchar="肆" + tmpnewchar ;break;
case "5": tmpnewchar="伍" + tmpnewchar ;break;
case "6": tmpnewchar="陆" + tmpnewchar ;break;
case "7": tmpnewchar="柒" + tmpnewchar ;break;
case "8": tmpnewchar="捌" + tmpnewchar ;break;
case "9": tmpnewchar="玖" + tmpnewchar ;break;

switch(part[0].length-i-1)
case 0: tmpnewchar = tmpnewchar +"元" ;break;
case 1: if(perchar!=0)tmpnewchar= tmpnewchar +"拾" ;break;
case 2: if(perchar!=0)tmpnewchar= tmpnewchar +"佰" ;break;
case 3: if(perchar!=0)tmpnewchar= tmpnewchar +"仟" ;break;
case 4: tmpnewchar= tmpnewchar +"万" ;break;
case 5: if(perchar!=0)tmpnewchar= tmpnewchar +"拾" ;break;
case 6: if(perchar!=0)tmpnewchar= tmpnewchar +"佰" ;break;
case 7: if(perchar!=0)tmpnewchar= tmpnewchar +"仟" ;break;
case 8: tmpnewchar= tmpnewchar +"亿" ;break;
case 9: tmpnewchar= tmpnewchar +"拾" ;break;

newchar = tmpnewchar + newchar;

if(Num.indexOf(".")!=-1)
if(part[1].length > 2)
part[1] = part[1].substr(0,2)

for(i=0;i<part[1].length;i++)
tmpnewchar = ""
perchar = part[1].charAt(i)
switch(perchar)
case "0": tmpnewchar="零" + tmpnewchar ;break;
case "1": tmpnewchar="壹" + tmpnewchar ;break;
case "2": tmpnewchar="贰" + tmpnewchar ;break;
case "3": tmpnewchar="叁" + tmpnewchar ;break;
case "4": tmpnewchar="肆" + tmpnewchar ;break;
case "5": tmpnewchar="伍" + tmpnewchar ;break;
case "6": tmpnewchar="陆" + tmpnewchar ;break;
case "7": tmpnewchar="柒" + tmpnewchar ;break;
case "8": tmpnewchar="捌" + tmpnewchar ;break;
case "9": tmpnewchar="玖" + tmpnewchar ;break;

if(i==0)tmpnewchar =tmpnewchar + "角";
if(i==1)tmpnewchar = tmpnewchar + "分";
newchar = newchar + tmpnewchar;


while(newchar.search("零零") != -1)
newchar = newchar.replace("零零", "零");
newchar = newchar.replace("零亿", "亿");
newchar = newchar.replace("亿万", "亿");
newchar = newchar.replace("零万", "万");
newchar = newchar.replace("零元", "元");
newchar = newchar.replace("零角", "");
newchar = newchar.replace("零分", "");
if (newchar.charAt(newchar.length-1) == "元" || newchar.charAt(newchar.length-1) == "角")
newchar = newchar+"整"
return newchar;

看了下没什么大问题本回答被提问者采纳

以上是关于JS中怎么把一个数字变成大写的啊的主要内容,如果未能解决你的问题,请参考以下文章

js怎么把数字金额转换为大写金额

js把日期变成一串数字

WPS表格怎么把小写字母直接变成大写

js怎么把20位数字字符串转换成数字,在线等

Java 关于中文大写金额与阿拉伯数字 互相转换的问题(eclipse版)

表格中如何把常规数字转换为小时分钟时间?