//Value being parsed at this point is "150.00"
var productValue = parseFloat($("#product-cost-value-value").text().replace(',', '.'));
console.log(productValue);
The value being logged is 150
.
However if I add some value to it, like this. The value changes to display the two decimals I want.
console.log(productValue + 0.01);
The value being logged is 150.01
How can I force my values to show two decimal places at all time?
//Value being parsed at this point is "150.00"
var productValue = parseFloat($("#product-cost-value-value").text().replace(',', '.'));
console.log(productValue);
The value being logged is 150
.
However if I add some value to it, like this. The value changes to display the two decimals I want.
console.log(productValue + 0.01);
The value being logged is 150.01
How can I force my values to show two decimal places at all time?
Share Improve this question edited Jun 18, 2012 at 13:39 mskfisher 3,4064 gold badges37 silver badges49 bronze badges asked Nov 8, 2011 at 13:49 Only Bolivian HereOnly Bolivian Here 36.7k65 gold badges166 silver badges257 bronze badges 2- possible duplicate of How to format a float in javascript? – Dave Newton Commented Nov 8, 2011 at 13:51
- possible duplicate of trim to 2 decimals – Phrogz Commented Nov 8, 2011 at 13:52
3 Answers
Reset to default 13use:
console.log(productValue.toFixed(2));
Use productValue.toFixed(2)
to show 2 decimal places
Use Number.prototype.toFixed(2)
(e.g. productValue.toFixed(2)
) to convert your number into a string formatted to two decimal places.