converting integer to float with precision 2 using Javascript or Jquery

Viewed 39231

I want to convert the integer to floating number with precision 2. ie. -

11 => 11.00

45 => 45.00

Please help me.

Thank you.

4 Answers

This a very common problem I see someone has already answered, I want to add to it, if you want to further use the number after conversion for calculations Try this in console

a = 10.2222;
console.log(typeof a) // "number"
console.log(a)        // 10.2222 

b = parseFloat(a).toFixed(2);
console.log(typeof b) // "String"
console.log(b)        // "10.22"

c = parseFloat(b)
console.log(typeof c) // "number"
console.log(c)        // 10.22

Explanation is-

toFixed() method outputs a string variable
but if you want to further use the value of b as a 'number'
you will have to, 
c = parseFloat(b)
typeof c
// "number"

Just add toFixed(2) here with your variable.

Related