Formatting a number with exactly two decimals in JavaScript

后端 未结 30 2229
广开言路
广开言路 2020-11-21 06:29

I have this line of code which rounds my numbers to two decimal places. But I get numbers like this: 10.8, 2.4, etc. These are not my idea of two decimal places so how I can

30条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 07:06

    (Math.round((10.2)*100)/100).toFixed(2)
    

    That should yield: 10.20

    (Math.round((.05)*100)/100).toFixed(2)
    

    That should yield: 0.05

    (Math.round((4.04)*100)/100).toFixed(2)
    

    That should yield: 4.04

    etc.

提交回复
热议问题