admin管理员组文章数量:1352292
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"
}
本文标签: javascriptJSON stringify conversion of float to intStack Overflow
版权声明:本文标题:javascript - JSON stringify conversion of float to int - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743911664a2560513.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论