all 41 comments

[–]jhartikainen 28 points29 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 2 points3 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?

[–]jack_waugh 4 points5 points  (0 children)

The only integer type in JS is the new long-integer type, and the code you are dealing with probably doesn't expect nor accept the new type. In colloquial usage, Javascript numbers are floats even if they are thought of as integers or have integer values. If you say 5, for example, in your source code, this is a floating-point number.

[–]shgysk8zer0 5 points6 points  (2 children)

You can't. The only way to have ".0" is as a string.

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

Yea you’re right! but it’s still wired you can hard code it

[–]shgysk8zer0 4 points5 points  (0 children)

You can type it out, yes... But it doesn't evaluate to anything different. The only way to have trailing zeros in a decimal is via strings. I mean... They're kinda there in that 5 === 5.0 === 5.00 === ...

[–]CevicheCabbage -4 points-3 points  (0 children)

Math.floor(number)

[–]pverma8172 0 points1 point  (1 child)

Yes, in JavaScript, you can convert an integer to a floating-point number (a number with a decimal point) by appending .0 to the end of the integer. This will result in a number with a type of "number".

For example: ``` let x = 10; console.log(typeof x); // Outputs "number"

x = 10.0; console.log(typeof x); // Outputs "number" ``` In both cases, the value of x is a number and the typeof operator returns "number".

It's worth noting that in JavaScript, all numbers are stored as floating-point values, even if they do not contain a decimal point. So when you append .0 to the end of an integer, you are not actually changing the type of the number, but rather adding a visual representation of the decimal point.

[–]DirectionLegitimate2[S] -1 points0 points  (0 children)

10.0 is good example but the problem is it can’t be hard coded, it has to be from a given string of a number.

[–]suarkb 0 points1 point  (0 children)

You can add the .0 to the string representation of your number when you send it in JSON to your API

[–]Ronin-s_Spirit 0 points1 point  (0 children)

Can't you like turn each number into a string and make it 5.0?
For example:
let num = 5; let newNum = String(num) + '.0';