26

I'm using the Number.prototype.toLocaleString() function to add commas to whole numbers. Documentation for it can be found here.

I am writing it as follows:

Number(data).ToLocaleString('en');

In Firefox/Chrome the number is displayed like 123,456,789. However, in IE it is displayed like 123,456,789.00.

1. Why is IE adding in the decimal point values?

2. How can I remove the decimal point values?

Rather than creating/using a custom function, I'm really just wondering if there is an option that I can add to ToLocaleString() like en, nodecimal. If that option is not available, I will consider a custom function.

neuquen
  • 3,991
  • 15
  • 58
  • 78
  • It really outputs that in Firefox ? – adeneo Feb 03 '14 at 20:25
  • 1) that's not valid JS. 2) `toLocaleString` is "implementation-dependent" according to the spec. – p.s.w.g Feb 03 '14 at 20:26
  • @adeneo Yep. Using v26 – neuquen Feb 03 '14 at 20:26
  • @p.s.w.g Woops. Added an extra period. Changed it. – neuquen Feb 03 '14 at 20:27
  • Don't forget to check the regional settings on your computer. I can change whether ie8 displays decimal points or not via "No. of digits after decimal". In windows 7, it's in control panel --> region and language --> formats --> additional settings --> numbers. ie8 seems to ignore the string supplied to toLocaleString (not against spec). – Harry Pehkonen Jun 04 '15 at 14:59
  • 1
    @HarryPehkonen. I understand that I could change my computer settings to achieve the desired affect personally, but my goal is to change it for all end users who come to my website. I have no control over their computer settings. – neuquen Oct 21 '15 at 00:49
  • There is an option you can use, it's called `maximumFractionDigits` and you need to set it to `0`. You need to set `minimumFractionDigits` as well. See specification [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString). For example: `Number(123534.34).toLocaleString('da-DK',{style:'currency',currency:"DKK",maximumFractionDigits:0, minimumFractionDigits:0});` will display `123.534 kr.` instead of `123.534,34 kr.` – Alex Bowyer Jan 16 '19 at 18:31
  • @AlexBowyer Thanks, but Guy already mentioned those two parameters in [his answer](https://stackoverflow.com/a/51779528/1751883) – neuquen Jan 23 '19 at 21:41

4 Answers4

36

How about toLocaleString:

const sum = 1000;

const formatted = sum.toLocaleString("en", {   
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
});

console.log(formatted);

for:

// 1,000

Or if you're into the money stuff:

const sum = 1000;

const formatted = sum.toLocaleString("en", {
    style: "currency",
    currency: "USD",
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
});

console.log(formatted);

for:

// $1,000

Replace "en" with one of the supported language tags*. For instance:

'en-US'
// en => a BCP 47 tag that represents a language
// US => a ISO_3166-1 Alpha-2 subtag that represents a country | optional

Replace "USD" with a ISO-4217 currency code. For instance:

'EUR'
// EUR is currency #978 on the active currencies codes list. 
// The result would be a numeric value prefixed by the € sign 

* Further information is available on the BCP 47 and ISO 3166-1 wikis.

Guy
  • 12,488
  • 16
  • 79
  • 119
  • Was `minimumFractionDigits` and `maximumFractionDigits` recently introduced? I don't believe that was an option back when I asked the question. – neuquen Aug 10 '18 at 16:53
  • @Keven You're probably right. From googling it, it seems that in 2015 it was already available https://stackoverflow.com/questions/31581011/how-to-use-tolocalestring-and-tofixed2-in-javascript so perhaps around a year after you asked – Guy Aug 11 '18 at 02:30
12

1) Refer to Andy E's (1) answer.

2) Andy E's solution works on IE 10 and lower but seems to cause the wrong outputs on modern browsers (try it in the console with any number). Here is a safer string manipulation:

Number(data).toLocaleString().split('.')[0];

*Andy I would have added this as a comment to your answer but I don't have enough reputation.

a15n
  • 497
  • 6
  • 16
  • 5
    This works fine when you know the separator is a dot ("."), what about commas as in French and others? – Mark Dineen Mar 10 '15 at 17:45
  • This will not work in certain locales. For example: `Number(123534.34).toLocaleString('da-DK',{style:'currency',currency:"DKK"})` which displays as `123.534,34 kr.`. Instead you should set the `maximumFractionDigits` and `minimumFractionDigits` options to `0`, as specified [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString). For example: `Number(123534.34).toLocaleString('da-DK',{style:'currency',currency:"DKK",maximumFractionDigits:0, minimumFractionDigits:0});` which will display `123.534 kr.` – Alex Bowyer Jan 16 '19 at 18:31
11

Which version of IE did you test in? In IE 10 and lower, toLocaleString is based on the ECMAScript specification, which states that the function should be "implementation dependant". In IE 11, it is based on the ECMA Internationalization API, and should be consistent with Firefox 26.

To remove the decimal values in IE 10 and lower (and potentially, other older browsers), you'll have to resort to string manipulation:

Number(data).toLocaleString('en').slice(0, -3);

There's also a polyfill available for this API, which will work for IE 10 and lower. Including it at the moment is a little tricky, since the browser/minified build contains no actual data (because it would be huge). The data is provided separately in JSON or JSONP format, so that you can download the correct data for the user currently browsing your site.

Andy E
  • 338,112
  • 86
  • 474
  • 445
  • I'm testing in IE9 and below and need to support IE 10 and below so it looks like I'll need to manipulate then. – neuquen Feb 03 '14 at 20:32
  • 2
    This will not work in certain locales. For example: `Number(123534.34).toLocaleString('da-DK',{style:'currency',currency:"DKK"})` which displays as `123.534,34 kr.`. Instead you should set the `maximumFractionDigits` and `minimumFractionDigits` options to `0`, as specified [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString). For example: `Number(123534.34).toLocaleString('da-DK',{style:'currency',currency:"DKK",maximumFractionDigits:0, minimumFractionDigits:0});` which will display `123.534 kr.` – Alex Bowyer Jan 16 '19 at 18:29
3

Number(data).toLocaleString().replace(/\D\d\d$/, ''); should cover any locale and browser.

Dovev Hefetz
  • 1,346
  • 14
  • 21
  • Great solution. Retains the thousand separator. and doesn't throw a tantrum if a number doesn't contain decimals (in later browsers). – user1830285 Jan 17 '17 at 10:31
  • This will not work in certain locales. For example: `Number(123534.34).toLocaleString('da-DK',{style:'currency',currency:"DKK"})` which displays as `123.534,34 kr.`. Instead you should set the `maximumFractionDigits` and `minimumFractionDigits` options to `0`, as specified [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString). For example: `Number(123534.34).toLocaleString('da-DK',{style:'currency',currency:"DKK",maximumFractionDigits:0, minimumFractionDigits:0});` which will display `123.534 kr.` – Alex Bowyer Jan 16 '19 at 18:32
  • In the OP's question, isn't that what he would want? He's only using whole numbers and IE is adding the .00 - or in your case ",00" – Dovev Hefetz Jan 23 '19 at 12:36