Format number to always show 2 decimal places

前端 未结 30 2697
爱一瞬间的悲伤
爱一瞬间的悲伤 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:46

    here is another solution to round only using floor, meaning, making sure calculated amount won't be bigger than the original amount (sometimes needed for transactions):

    Math.floor(num* 100 )/100;
    

提交回复
热议问题