Format number to always show 2 decimal places

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

    You are not giving us the whole picture.

    javascript:alert(parseFloat(1).toFixed(2)) shows 1.00 in my browsers when I paste it int0 the location bar. However if you do something to it afterwards, it will revert.

    var num = 2
    document.getElementById('spanId').innerHTML=(parseFloat(num).toFixed(2)-1)
    
    
    shows 1 and not 1.00
    

提交回复
热议问题