How can we convert a JavaScript string variable to decimal?
Is there such a function as
parseInt(document.getElementById(amtid4).innerHTML)
Yes - parseFloat .
parseFloat
parseFloat(document.getElementById(amtid4).innerHTML);
To format numbers, use toFixed :
toFixed
var num = parseFloat(document.getElementById(amtid4).innerHTML).toFixed(2);
num now a string with a number formatted with two decimal places.
num
You can also use the constructor / function Number (there is no need for a radius and can be used for both integers and float):
Number
Number('09'); /=> 9 Number('09.0987'); /=> 9.0987
It works:
var num = parseFloat(document.getElementById(amtid4).innerHTML, 10).toFixed(2);
var formatter = new Intl.NumberFormat("ru", { style: "currency", currency: "GBP" }); alert( formatter.format(1234.5) ); // 1 234,5 £
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
An easy short way would be to use + x. It keeps the sign unchanged, as well as decimal numbers. Another alternative is to use parseFloat (x). The difference between parseFloat (x) and + x for the empty string + x returns 0 when parseFloat (x) returns NaN.