Convert to currency format

后端 未结 6 1179
迷失自我
迷失自我 2020-12-06 03:43

Is there a built in function of JavaScript to convert a string into a currency format?

For example

var a = \'1234\';
a.convertToCurrency();   // retu         


        
6条回答
  •  萌比男神i
    2020-12-06 04:12

    Although this is an old post. Nonetheless, this worked in for me in my own case:

    var a = parseInt("1234");
    console.log("$"+number_format(a));
    
    var number_format = function(total) {
       return total.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
    };
    

提交回复
热议问题