在javascript中使用十进制格式化数字 [英] Formatting Number in javascript using decimal
本文介绍了在javascript中使用十进制格式化数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
a = 12345678
b = 12345678.098
c = 12345678.1
d = 12345678.11
我需要格式化, p>
a = 12,345,678.000
b = 12,345,678.098
c = 12,345,678.100
d = 12,345,678.110
我已经尝试了tolocaleString()和toFixed(3)方法。但是我不能同时使用这两种方法。
需要您的建议。
解决方案
var num = 123456789; console.log(num.toLocaleString(undefined,{minimumFractionDigits:3,maximumFractionDigits :3}));
undefined
将使用用户的语言环境,您可能想指定一个自定义的。
更新回答
< data-lang =jsdata-hide =falsedata-console =truedat a-babel =false>
var number = 12345678; console.log (parseInt函数(数* 1000).toLocaleString( EN-US)代替(/ \,([^ \,] *)$ /,+ '$ 1')。 );
I have following types of values in a, b,c,d.
a= 12345678
b= 12345678.098
c=12345678.1
d=12345678.11
I need to format like,
a = 12,345,678.000
b= 12,345,678.098
c=12,345,678.100
d=12,345,678.110
I already tried tolocaleString() and toFixed(3) method. But I'm not able to works together of both method.
need your suggestion on this.
解决方案
var num = 123456789;
console.log(num.toLocaleString(undefined, {minimumFractionDigits: 3, maximumFractionDigits: 3}));
undefined
will use user's locale, you might want to specify a custom one.
Updated answer
var number = 12345678;
console.log(parseInt(number*1000).toLocaleString("en-US").replace(/\,([^\,]*)$/,"."+'$1'));
这篇关于在javascript中使用十进制格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文