153
rated 0 times
[
154]
[
1]
/ answers: 1 / hits: 58323
/ 9 Years ago, sun, february 8, 2015, 12:00:00
Let me give you an example.
var a = 2.0;
var stringA = + a;
I will get: stringA = 2
, but I want: stringA = 2.0
.
I don't want to lose precision however, so if:
var b = 2.412;
var stringB = + b;
I want to get the standard: stringB = 2.412
.
That's why toFixed()
won't work here. Is there any other way to do it, than to explicitly check for whole numbers like this?:
if (a % 1 === 0)
return + a + .0;
else
return + a;
More From » javascript