JavaScript正则表达式把数字number转换为字母/符号/中文货币

这种方式一般是用来做金钱大小写的;

大写数字书写规范汉字大写数字书写如下: 零、壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿 大写金额数字到元或者角为止的

具体的代码如下

var num='0123456789';
var num2='206523846589634858123';
var reg=/(\d)/g;
var targetStr1=num.replace(reg,function ($1) {
return "零壹贰叁肆伍陆柒捌玖".split("")[$1];
});
var targetStr2=num2.replace(reg,function ($1) {
return "零壹贰叁肆伍陆柒捌玖".split("")[$1];
});
console.log(targetStr1);//ABCDEFGHIJ
console.log(targetStr2);//CAGFCDIEGFIJGDEIFIBCD

如果您是吧0123456789替换为别的;

直接在里面进行修改就可以了;

下面是一种吧数字转为货币的写法,供参考

    function formatMoney(number, places, symbol, thousand, decimal) {
        number = number || 0;
        places = !isNaN(places = Math.abs(places)) ? places : 2;
        symbol = symbol !== undefined ? symbol : "$";
        thousand = thousand || ",";
        decimal = decimal || ".";
        var negative = number < 0 ? "-" : "",
            i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
            j = (j = i.length) > 3 ? j % 3 : 0;
        return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
    }

    // To create it as a library method:
    myLibrary.formatMoney = function (number, places, symbol, thousand, decimal) {
        /* as above */
    }

    // Example usage:
    formatMoney(54321); // $54,321
    myLibrary.formatMoney(12345, 0, "£ "); // £ 12,345

另外一种写法

<script>
function cc(s){
if(/[^0-9\.]/.test(s)) return "invalid value";
s=s.replace(/^(\d*)$/,"$1.");
s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");
s=s.replace(".",",");
var re=/(\d)(\d{3},)/;
while(re.test(s))
s=s.replace(re,"$1,$2");
s=s.replace(/,(\d\d)$/,".$1");
return "¥" + s.replace(/^\./,"0.")
}
</script>
<input onchange="this.value=cc(this.value)">

1.用循环的方式实现

function formatNum(str){
var newStr = "";
var count = 0;

if(str.indexOf(".")==-1){
for(var i=str.length-1;i>=0;i--){
if(count % 3 == 0 && count != 0){
newStr = str.charAt(i) + "," + newStr;
}else{
newStr = str.charAt(i) + newStr;
}
count++;
}
str = newStr + ".00"; //自动补小数点后两位
console.log(str)
}
else
{
for(var i = str.indexOf(".")-1;i>=0;i--){
if(count % 3 == 0 && count != 0){
newStr = str.charAt(i) + "," + newStr; //碰到3的倍数则加上“,”号
}else{
newStr = str.charAt(i) + newStr; //逐个字符相接起来
}
count++;
}
str = newStr + (str + "00").substr((str + "00").indexOf("."),3);
console.log(str)
}
}

formatNum('13213.24'); //输出13,213.34
formatNum('132134.2'); //输出132,134.20
formatNum('132134'); //输出132,134.00
formatNum('132134.236'); //输出132,134.236

2.使用正则(比较不足的是还是得自己去判断小数点后面的位数,有更智能的正则请通知我~)

function regexNum(str){
var regex = /(\d)(?=(\d\d\d)+(?!\d))/g;

if(str.indexOf(".") == -1){

str= str.replace(regex,',') + '.00';
console.log(str)

}else{
var newStr = str.split('.');
var str_2 = newStr[0].replace(regex,',');

if(newStr[1].length <= 1){
//小数点后只有一位时
str_2 = str_2 + '.' + newStr[1] +'0';
console.log(str_2)

}else if(newStr[1].length > 1){
//小数点后两位以上时
var decimals = newStr[1].substr(0,2);
var srt_3 = str_2 + '.' + decimals;
console.log(srt_3)
}
}
}
regexNum('23424224'); //输出2,42,224.00
regexNum('23424224.2'); //输出2,42,224.20
regexNum('23424224.22'); //输出2,42,224.22
regexNum('23424224.233'); //输出2,42,224.23

~

未经允许不得转载:WEB前端开发 » JavaScript正则表达式把数字number转换为字母/符号/中文货币

赞 (1)