Internationalization(Number formatting “num.toLocaleString()”) not working for chrome

前端 未结 4 757
自闭症患者
自闭症患者 2020-12-07 03:12

i want do number formatting in Javascript.. and i use the following method num.toLocaleString() which will work for Firefox, IE but doesnt work for Google Chrome.. Wat i nee

4条回答
  •  無奈伤痛
    2020-12-07 04:04

    A bit of voodoo can implement your own number formatting. You could build this into String.prototype, but I didnt want that, since its localized.

    function reverse(str) {
        return str.split('').reverse().join(''); 
    }
    
    function num2str(num) {
        var str = num+"";
        // european
        // return reverse(reverse(str.replace('.',',')).replace(/\d{3}/g,'$&.').replace(/\.$/,''));
        // american
        return reverse(reverse(str).replace(/\d{3}/g,'$&,').replace(/\,$/,''));
    }
    

    and then its

    > console.log(25000.45)
    > 25,000.45
    

提交回复
热议问题