Convert JavaScript string variable to decimal / monetary

How can we convert a JavaScript string variable to decimal?

Is there such a function as

parseInt(document.getElementById(amtid4).innerHTML) 
+82
javascript string-conversion
May 23 '11 at 10:14
source share
5 answers

Yes - parseFloat .

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



To format numbers, use toFixed :

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

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

+189
May 23 '11 at 10:16
source share

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('09'); /=> 9 Number('09.0987'); /=> 9.0987 
+52
May 23 '11 at 10:32
source share

It works:

 var num = parseFloat(document.getElementById(amtid4).innerHTML, 10).toFixed(2); 
+8
Dec 20 '12 at 12:33
source share
 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

+3
Nov 28 '15 at 8:34
source share

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.

+1
Jun 29 '14 at 19:06
source share



All Articles