Format number to always show 2 decimal places

前端 未结 30 2851
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-21 08:17

I would like to format my numbers to always display 2 decimal places, rounding where applicable.

Examples:

number     display
------     -------
1            


        
30条回答
  •  礼貌的吻别
    2020-11-21 08:34

    Just run into this one of longest thread, below is my solution:

    parseFloat(Math.round((parseFloat(num * 100)).toFixed(2)) / 100 ).toFixed(2)
    

    Let me know if anyone can poke a hole

提交回复
热议问题