convert a JavaScript string variable to decimal/money

JavascriptDecimalString Conversion

Javascript Problem Overview


How can we convert a JavaScript string variable to decimal?

Is there a function such as:

parseInt(document.getElementById(amtid4).innerHTML)

Javascript Solutions


Solution 1 - Javascript

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

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

Alternatively like Andy E said in the comments you can use + for conversion

+'09'; /=> 9
+'09.0987'; /=> 9.0987

Solution 3 - Javascript

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 4 - Javascript

This works:

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

Solution 5 - Javascript

An easy short hand way would be to use +x It keeps the sign intact as well as the decimal numbers. The other alternative is to use parseFloat(x). Difference between parseFloat(x) and +x is for a blank string +x returns 0 where as parseFloat(x) returns NaN.

Solution 6 - Javascript

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

I made a little helper function to do this and catch all malformed data

function convertToPounds(str) {	
    var n = Number.parseFloat(str);
    if(!str || isNaN(n) || n < 0) return 0;
    return n.toFixed(2);
}

Demo is here

Solution 8 - Javascript

Prefix + could be used to convert a string form of a number to a number (say "009" to 9)

const myNum = +"009"; // 9

But be careful if you want to SUM 2 or more number strings into one number.

const myNum = "001" + "009";   // "001009"  (NOT 10)

const myNum = +"001" + +"009"; // 10

Alternatively you can do

const myNum = Number("001") + Number("009"); // 10

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVaradaView Question on Stackoverflow
Solution 1 - JavascriptlonesomedayView Answer on Stackoverflow
Solution 2 - JavascriptKooiIncView Answer on Stackoverflow
Solution 3 - JavascriptzloctbView Answer on Stackoverflow
Solution 4 - JavascriptSanjayView Answer on Stackoverflow
Solution 5 - JavascriptTusharView Answer on Stackoverflow
Solution 6 - JavascriptdondrzzyView Answer on Stackoverflow
Solution 7 - JavascriptsidonaldsonView Answer on Stackoverflow
Solution 8 - JavascriptSridharKrithaView Answer on Stackoverflow