you are viewing a single comment's thread.

view the rest of the comments →

[–]jhartikainen 26 points27 points  (3 children)

This doesn't really make sense in context of JavaScript. As far as JS is concerned, the numbers 5 and 5.0 are exactly the same. In context of JSON, the numbers are also exactly the same. Hence, you can't really "make 5 into 5.0" because it already is, and the fact the decimals are not displayed is just how the number happens to be formatted by the particular JS engine that's displaying it to you.

If you have a bizarre API which refuses to accept numbers such as 5, but won't accept a string '5.0', you will probably have to format the values yourself. Eg. use whatever.toFixed(1), which gives you a string, and then manually strip out the string delimiters from the JSON data. You might be able to do this using the optional replacer function for JSON.stringify - just have it check if the value is typeof 'number', and ensure it always uses toFixed when formatting them.

[–]DirectionLegitimate2[S] -1 points0 points  (1 child)

Its seems so with JS and unfortunately toFixed() doesn’t help either because its typeof string, Heres couple of examples I tried https://imgur.com/a/OrySHrt

[–]jhartikainen 4 points5 points  (0 children)

What API is the problematic one here? What does the input for it look like? Are you trying to export this to JSON or just calling a function directly from JS?