How to parse float with two decimal places in javascript?

user357034 picture user357034 · Dec 14, 2010 · Viewed 654.7k times · Source

I have the following code. I would like to have it such that if price_result equals an integer, let's say 10, then I would like to add two decimal places. So 10 would be 10.00. Or if it equals 10.6 would be 10.60. Not sure how to do this.

price_result = parseFloat(test_var.split('$')[1].slice(0,-1));

Answer

Mahesh Velaga picture Mahesh Velaga · Dec 14, 2010

You can use toFixed() to do that

var twoPlacedFloat = parseFloat(yourString).toFixed(2)