'converting integer to float with precision 2 using Javascript or Jquery
I want to convert the integer to floating number with precision 2. ie. -
11 => 11.00
45 => 45.00
Please help me.
Thank you.
Solution 1:[1]
Solution 2:[2]
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"
Solution 3:[3]
Just add toFixed(2) here with your variable.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user1236048 |
| Solution 2 | vegemite4me |
| Solution 3 | Abdus Salam Azad |
