I am using Node.js and the built-in JSON object to stringify a JSON object. In the object is
{
weight : 1.0
}
However when I stringify and write to a file the output is weight : 1.
I am using Node.js and the built-in JSON object to stringify a JSON object. In the object is
{
weight : 1.0
}
However when I stringify and write to a file the output is weight : 1.
Share Improve this question asked May 21, 2013 at 4:13 kyleEDkyleED 2,3973 gold badges19 silver badges23 bronze badges 8-
2
Not sure what the issue is.
1
and1.0
are the same thing in JavaScript. – phenomnomnominal Commented May 21, 2013 at 4:15 -
use
parseInt(obj.weight)
– Heavy Commented May 21, 2013 at 4:25 - Try with 1.5 and you'll see it infact keeps the decimals.. – techfoobar Commented May 21, 2013 at 4:29
- 3 The ment is the file generated is being used as an input for another system (Java) that cannot parse ints to double. Therefore the output format is required to be in 1.0 – kyleED Commented May 21, 2013 at 4:30
- 1 @kyleED Parse it into a float before reading in the Java tool – Prasath K Commented May 21, 2013 at 4:39
2 Answers
Reset to default 4As noted in this answer to a similar question, and on this MSDN page:
There is no such thing as an integer in JavaScript. Numbers in JavaScript are "double-precision 64-bit format IEEE 754 values".
Open up your web browser's console and type 1.0
. You'll see 1
printed out. All numbers in JavaScript are floating point numbers, so your serializer simply chose to leave off unnecessary precision.
Actually yours is not an issue , 1 == 1.0 == 1.00
in Javascript and if you have a float value like 1.55
then stringify gives you the same 1.55 not 1
.. Even then if you want 1.0
to be written , change the value into string
I mean Enclose the value in double quotes
{
weight : "1.0"
}