Format number to always show 2 decimal places

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

    Convert a number into a string, keeping only two decimals:

    var num = 5.56789;
    var n = num.toFixed(2);
    

    The result of n will be:

    5.57
    

提交回复
热议问题