# convert a JavaScript string variable to decimal/money

## convert a JavaScript string variable to decimal/money

How can we convert a JavaScript string variable to decimal?
Is there a function such as
parseInt(document.getElementById(amtid4).innerHTML)

### Solution 1:

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.

### Solution 2:

You can also use the `Number` constructor/function (no need for a radix and usable for both integers and floats):

``````Number('09'); /=> 9
Number('09.0987'); /=> 9.0987
``````

### Solution 3:

This works:

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

### Solution 4:

``````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

### Solution 5:

It is fairly risky to rely on javascript functions to compare and play with numbers. In javascript (0.1+0.2 == 0.3) will return false due to rounding errors. Use the math.js library.

### Solution 6:

An easy short hand way would be to use +x