简体   繁体   English

在Javascript中格式化小数位的问题

[英]Problem with formating decimal places in Javascript

I have a problem with decimal places.我有小数位问题。 My code gives me the result 123 123 12, while I need to display the result in the form 12 312 312.我的代码给出了结果 123 123 12,而我需要以 12 312 312 的形式显示结果。

Can anyone help setting this formatting?任何人都可以帮助设置这种格式吗?

Code here:代码在这里:

        var fcqInteger = parseInt(fcq.replace(/\s/g, ''));
        valPrice = parseFloat(valPrice.replace(',', '.'));
        var marketCap = (fcqInteger * valPrice)+'';
        // result
        var marketCapParts = marketCap.match(/[\s\S]{1,3}/g) || []; 
        marketCap = marketCapParts.join(' '); 

Thanks for help.感谢帮助。

You could replace with space by looking to the end of the string for groupd of three.您可以通过查看三个一组的字符串末尾来替换为空格。

 var data = '12312312' console.log(data.replace(/.{1,3}(?=(...)+$)/g, '$& ')); // replace console.log(data.match(/.{1,3}(?=(...)*$)/g).join(' ')); // match/join

You can use Number.toLocaleString and then replace the , with spaces.您可以使用Number.toLocaleString ,然后将,替换为空格。

 let num = '123 123 12'; let out = Number(num.replace(/\\s/g, '')).toLocaleString('us-US', {maximumFractionDigits: 5}).replace(/,/g, ' '); console.log(out)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM