admin管理员组文章数量:1415139
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 num.toString() // outputs 0
num2.valueOf() or num2.toString() // outputs 0.01
Is this normal behavior and is there someway to retain the trailing 0s?
EDIT: I changed my original question because I realized after some testing that the above is root of the problem. Thanks.
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 num.toString() // outputs 0
num2.valueOf() or num2.toString() // outputs 0.01
Is this normal behavior and is there someway to retain the trailing 0s?
EDIT: I changed my original question because I realized after some testing that the above is root of the problem. Thanks.
Share Improve this question edited May 20, 2010 at 19:02 Choy asked May 20, 2010 at 15:31 ChoyChoy 2,11711 gold badges39 silver badges49 bronze badges 6- When you say that it "doesn't work properly", what goes wrong? Have you tried debugging it in some way? – Pointy Commented May 20, 2010 at 15:47
- the format function is supposed to use the 'style' variable to decide how many decimals it should output. When I use the first method it cuts off the decimals, but outputs correctly using the second method. The format function itself works just fine, it's only when I'm passing the style in the above manner so I'm just wondering if there's a difference between having .toString inside the format() (method 1) part or outside (method 2). – Choy Commented May 20, 2010 at 15:55
- Well have you inserted some "console.log" calls (for Firebug) or some alerts or whatever to see what that "typeof" thing is returning, and what the result of that "toString" is when called in the if statement? – Pointy Commented May 20, 2010 at 15:59
-
Can you include how you are calling
formatNumber
? – John Keyes Commented May 20, 2010 at 16:12 - @John Keyes, added more detail above. @Pointy I tried alerting the style after changing it a string and it returns object expected error. – Choy Commented May 20, 2010 at 16:32
2 Answers
Reset to default 4It is not toString
nor valueOf
that truncates trailing 0s after a decimal!
When you write a decimal this way:
var num2 = 0.0100
you are telling your interpreter that variable num2 should contain decimal number 0.0100, i.e. 0.01 since the last two zeros are not significant.
The decimal number is memory represented as a decimal number:
0.0100
0.010
0.01
0.01000
are all the very same number and so they are all represented the same way in memory. It is not possible to distinguish among them.
So it is not possible to know if num2 value 0.01 has been assigned writing that number with zero, one, two or more trailing zeros.
If you want to store a decimal number the way it is written then you have to store it as a string.
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'
本文标签: javascripttoString and valueOf truncates trailing 0s after decimalStack Overflow
版权声明:本文标题:javascript - toString and valueOf truncates trailing 0s after decimal - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745200330a2647338.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论