133

How can we convert a JavaScript string variable to decimal?

Is there a function such as:

parseInt(document.getElementById(amtid4).innerHTML)
Zeeshan Hassan Memon
  • 8,105
  • 4
  • 43
  • 57
Varada
  • 16,026
  • 13
  • 48
  • 69

8 Answers8

276

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.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • 1
    Can we make that variable with 2 decimal points such as 120.50? right now I am getting 120.5 only. – Varada May 23 '11 at 10:27
  • 21
    To quote @PhilipWhitehouse "Please, to anyone reading this in the future, do not use float to store currency. You will loose precision and data. You should store it as a integer number of cents (or pennies etc.) and then convert prior to output.". Source: http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript – SSH This Nov 27 '13 at 17:33
  • @SSHThis As far as I'm aware JavaScript doesn't have an integer type. It only has a Number type which are all stored internally as 64bit floating point numbers. Your point is valid for other languages that do have integer types such as C#, C++, but not really for JavaScript. – Simon Brangwin Feb 11 '15 at 04:06
  • 1
    @SimonBrangwin His point is still valid, but the semantics can be confusing. What he really means is "only store currency as whole numbers in javascript since they'll be stored as floating point numbers". – Josh Noe Aug 07 '17 at 00:21
  • 1
    Just a reminder: `parseFloat('22w')` is `22` and `parseFloat('w22')` is `NaN` – I.G. Pascual Nov 15 '17 at 11:18
  • First Link is broken. – pungggi Dec 27 '20 at 21:41
  • Thanks, @pungggi: both links now fixed. – lonesomeday Dec 28 '20 at 16:37
  • When I try `parseFloat("11.0")` it results in 11 instead of 11.0 – RixTheTyrunt Jul 06 '22 at 12:04
  • @RixTheTyrunt in what way is this not what you expect? – lonesomeday Jul 09 '22 at 10:28
  • It's just weird that JavaScript erases the `.0` part – RixTheTyrunt Jul 10 '22 at 12:55
76

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
KooiInc
  • 119,216
  • 31
  • 141
  • 177
19
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

zloctb
  • 10,592
  • 8
  • 70
  • 89
12

This works:

var num = parseFloat(document.getElementById(amtid4).innerHTML, 10).toFixed(2);
Eitan T
  • 32,660
  • 14
  • 72
  • 109
Sanjay
  • 121
  • 1
  • 2
2

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.

Tushar
  • 469
  • 1
  • 7
  • 14
2

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.

dondrzzy
  • 182
  • 1
  • 7
2

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

const convertToPounds = (str = "", asNumber = false) => {
  let num = Number.parseFloat(str);
  if (isNaN(num) || num < 0) num = 0;
  if (asNumber) return Math.round(num * 1e2) / 1e2
  return num.toFixed(2);
};

Demo is here

sidonaldson
  • 24,431
  • 10
  • 56
  • 61
0

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
SridharKritha
  • 8,481
  • 2
  • 52
  • 43