Given a variable
var str = 1;
convert str to output 1.00 as a number and not string.
so the output should be 1.00 and not "1.00"
what javascript operations should I use to do this?.
str.toFixed(2) returns a string and not a number so please..
Given a variable
var str = 1;
convert str to output 1.00 as a number and not string.
so the output should be 1.00 and not "1.00"
what javascript operations should I use to do this?.
str.toFixed(2) returns a string and not a number so please..
When you're talking about numbers, there is no difference between 1
and 1.00
. So, if it's a number, Javascript will treat them the same. If you want it as 1.00
, the only real way to do that is by creating a string of it with something like:
var nnn = 1;
var sss = nnn.toFixed(2));
The presentation of that number (either as a string or direct to output) may be under your control but the number itself is not (other than changing the value of course but, as already mentioned, there is no difference between the values 1
, 1.0
or 1e0
).
JavaScript only has one kind of number: "number", which is a IEEE-754 Double Precision
See this question to format to 2 decimal places.