I have some string values like "35.5" , "32.20" and I want them to be converted to numbers but keep the exact same decimals. When I use Number("32.0") for example I get 32 but I want 32.0. If I convert Number("35.5") I want 35.5 not 35.50, is there any way to do this easily?
Asked
Active
Viewed 211 times
0
-
No, because in javascript `25.0` _is the same as_ `25`. The decimal `.0` is only for textual output. – evolutionxbox May 17 '22 at 07:13
-
possible duplicate - https://stackoverflow.com/a/4868643/3183454 – the.marolie May 17 '22 at 07:16
1 Answers
0
if you want a fixed number of floating point you can use toFixed
but be aware that returns a string
const strings = [ "35.5" , "32.20", "32.0"]
const result = strings.map(n => parseFloat(n).toFixed(1))
console.log(result)

R4ncid
- 6,944
- 1
- 4
- 18
-
the problem is that I have an object with keys like "35.5" but I have to make some math operations on these keys so I have to cast them to numbers. Later I want to get the objects by passing in the keys again and if the key was 35.5 but my casted number turns out as 35 I dont get the object in return. – headliner090 May 17 '22 at 07:17
-
@headliner090 can you add the operation that returns 35 instead of 35.5? probably there is something wrong there – R4ncid May 17 '22 at 07:19