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.
JSON.stringify() converts a value to JSON notation representing it: Boolean , Number , String , and BigInt (obtainable via Object() ) objects are converted to the corresponding primitive values during stringification, in accordance with the traditional conversion semantics.
The JSON.stringify() function will convert any dates into strings.
stringify converts DateTimeOffset to UTC format #1710.
JSON. parse is the opposite of JSON. stringify .
As 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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With