-1

I have a number 1000 in an object.

I want it to be in 1,000.00 format

I tried:

obj[key] = (parseFloat(obj[key]).toFixed(2)).toLocaleString();

This gives 1000.00

d

Le guy
  • 59
  • 8
  • i have done what that answers says but i dont get it – Le guy Mar 11 '19 at 13:37
  • Read the documentation of `toLocaleString`. It has an option to group digits –  Mar 11 '19 at 14:00
  • @Amy where is it? – Le guy Mar 11 '19 at 14:09
  • Google "mdn toLocaleString" –  Mar 11 '19 at 14:11
  • @Amy i cant check now because the back end is down. but you meant `toLocaleString(undefined, {minimumFractionDigits: 2})` right – Le guy Mar 11 '19 at 14:32
  • 2
    @deceze Maybe there's a better duplicate? The requirement to include two decimal places even if the number to be formatted is a whole number is not present in the linked duplicate. There happens to be [an answer](https://stackoverflow.com/a/47565687/1709587) that does so anyway, but it's a long way down the page after lots of answers that don't satisfy the specific requirements here. (It's also arguably a bad answer to the question it's posted on, and is copied from the source linked without formatting as a quote, in violation of our referencing policy.) I'm tentatively voting to reopen this. – Mark Amery Mar 11 '19 at 14:39
  • @Leguy Yes, that and the `useGrouping` option. –  Mar 11 '19 at 14:44

1 Answers1

0

Edited answer to account for numbers with no remainder:

Use the options in toLocaleString();

Try this:

obj[key] = obj[key].toLocaleString(undefined,{minimumFractionDigits: 2, maximumFractionDigits: 2});

No need to parse as float in this case. Hope this helps.

Sam Creamer
  • 5,187
  • 13
  • 34
  • 49