search

Home  >  Q&A  >  body text

How to convert currency string to floating point number using Javascript

<p>I have a textbox that will have a <em>currency</em> string in it and I need to convert that string to a double precision float to perform some operations. </p> <p><code>"$1,100.00"</code> → <code>1100.00</code></p> <p>This needs to be done on the client side. I have no choice but to keep the <em>Currency</em> string as input, but need to cast it to a double precision float for some math operations. </p>
P粉593118425P粉593118425499 days ago723

reply all(2)I'll reply

  • P粉080643975

    P粉0806439752023-08-24 20:31:11

    accounting.js is a good choice. I'm using it on a project and had a very good experience using it.

    accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99
    accounting.unformat("€ 1.000.000,00", ","); // 1000000

    You can find it on GitHub.

    reply
    0
  • P粉208469050

    P粉2084690502023-08-24 14:10:15

    Remove all non-decimal points/digits:

    var currency = "-,400.50";
    var number = Number(currency.replace(/[^0-9.-]+/g,""));

    reply
    0
  • Cancelreply