formatting - How to format big numbers in JavaScript? - Stack Overflow

I have the following big number: 2.9364136545300044e+24 I heed to format this value to 2,936,413.65 val

I have the following big number: 2.9364136545300044e+24 I heed to format this value to 2,936,413.65 value

How could I do it of are there any libraries to make it?

I have the following big number: 2.9364136545300044e+24 I heed to format this value to 2,936,413.65 value

How could I do it of are there any libraries to make it?

Share asked Oct 30, 2016 at 16:44 ErikErik 14.8k49 gold badges140 silver badges223 bronze badges 4
  • Convert the number to a String, and add a ma every third character from the right? – Carcigenicate Commented Oct 30, 2016 at 17:00
  • Yes. But I need to take care for smaller numbers also – Erik Commented Oct 30, 2016 at 17:02
  • Why would my suggestion not work for smaller numbers? – Carcigenicate Commented Oct 30, 2016 at 17:03
  • Format numbers in Javascript – jfriend00 Commented Oct 30, 2016 at 17:30
Add a ment  | 

2 Answers 2

Reset to default 3

You can use Intl.NumberFormat.prototype.format, String.prototype.slice(), String.prototype.concat()

var number = 2.9364136545300044e+24;
var n = new Intl.NumberFormat().format(number);
var res = n.slice(0, 9).concat(".").concat(n.slice(10, 12));
console.log(res);

As a quick and easy solution you can use number.toLocaleString().

Since, the localString is based on Browser definition different users may get a different results. However, you can force a specific location to always get the same format. Ex: toLocaleString('en-GB', { timeZone: 'UTC' })

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744857773a4597510.html

相关推荐

  • formatting - How to format big numbers in JavaScript? - Stack Overflow

    I have the following big number: 2.9364136545300044e+24 I heed to format this value to 2,936,413.65 val

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信