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
 |  Show 1 more ment

2 Answers 2

Reset to default 4

It 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