Format number to always show 2 decimal places

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

    Are you looking for floor?

    var num = 1.42482;
    var num2 = 1;
    var fnum = Math.floor(num).toFixed(2);
    var fnum2 = Math.floor(num2).toFixed(2);
    alert(fnum + " and " + fnum2); //both values will be 1.00
    

提交回复
热议问题