Reputation: 2551
I noticed that if I do in JavaScript:
var dict = { "foo" : 1.0 };
when retrieving dict
I will get:
{ foo : 1 }
What is the rationale behind this? And what is the preferred way of avoiding it (I currently convert 1.0
to a string)?
Upvotes: 0
Views: 506
Reputation: 413737
JavaScript doesn't convert floats to integers. What you're seeing is the behavior of some diagnostic tool, whatever it is you're using to look at the value of that variable. JavaScript numbers are always double-precision floating point, except in the middle of some bitwise operations (and that's a transient condition).
Because all numbers are floating point, it's not necessary to use an explicit decimal when initializing a variable to a value that's got no fractional part. That is, 1
and 1.0
are exactly the same value in a JavaScript program.
Upvotes: 4