toString and valueOf truncates trailing 0s after decimal

后端 未结 2 1857
南旧
南旧 2021-01-13 18:27

In javascript, I\'ve noticed that toString and valueOf truncates trailing 0s after a decimal. For example:

var num = 0.00
var num2 = 0.0100

num.valueOf() or         


        
2条回答
  •  攒了一身酷
    2021-01-13 19:16

    A number in javascript does not have trailing zeros- if it could, where would you stop? That is normal behavior. You can force them to appear if you return a string-

    var n= '0.0'
    alert(n)>> 0
    alert(n.toFixed(5))>> '0.00000'
    alert(n.toPrecision(5))>>'0.0000'
    

提交回复
热议问题