convert a JavaScript string variable to decimal/money

Varada picture Varada · May 23, 2011 · Viewed 297.6k times · Source

How can we convert a JavaScript string variable to decimal?

Is there a function such as:

parseInt(document.getElementById(amtid4).innerHTML)

Answer

lonesomeday picture lonesomeday · May 23, 2011

Yes -- parseFloat.

parseFloat(document.getElementById(amtid4).innerHTML);

For formatting numbers, use toFixed:

var num = parseFloat(document.getElementById(amtid4).innerHTML).toFixed(2);

num is now a string with the number formatted with two decimal places.