0

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..

patz
  • 1,306
  • 4
  • 25
  • 42

2 Answers2

2

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).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

JavaScript only has one kind of number: "number", which is a IEEE-754 Double Precision

See this question to format to 2 decimal places.

Community
  • 1
  • 1
mhatch
  • 4,441
  • 6
  • 36
  • 62
  • the question is not more on formatting issue, is more about the results type to be a number and not string. which is not possible as answered above – patz May 31 '16 at 02:44
  • Yes, the only kind of 'number' in js is "number" as specified by the IEEE-754 Double Precision specs. – mhatch May 31 '16 at 02:46