Search code examples
javascriptnumbers

Javascript numbers with rounding decimals


I'm trying to format numbers like this with no luck. Can someone help me out with this ? This is what I want to do: this number 42050 to be 421 this 60480=605, 158600=1,586, 175304=1,753, 117349440=1,173,494 and so on.

I've tried using the Intl.formatNumber('en-EN', {...options}).format(value) with different options with no luck.


Solution

  • You can round the numbers as described by dividing by 100 and rounding:

    console.log(Math.round(158600/100)); // 1586
    

    You can then apply the number formatter to the results to get the commas.