Function to convert between currency (formatted) and float (non-formatted)

Posted on 2007-07-31
Last Modified: 2012-06-27

I am looking for two functions.  One that can be passed a floating point number (ex: 1234.56) and returns the number formatted as currency ($1,234.56) AND the reverse function ($1,234.56 -> 1234.56)

I'm (nearly) certain that the solution would make use of regular expressions, so even just having the proper expressions would be adequate

Validation of the input is not necessary (I can do that separately) ...

Question by:Argblat
    LVL 5

    Accepted Solution

    function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    return (((sign)?'':'-') + '$' + num + '.' + cents);

    to do the reverse, just replace the '$' and the ',' with an empty string ("");

    something like this.value.replace("$","").replace(",","")
    LVL 4

    Assisted Solution

    function formatCurrency(strValue)
          strValue = strValue.toString().replace(/\$|\,/g,'');
          dblValue = parseFloat(strValue);

          blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
          dblValue = Math.floor(dblValue*100+0.50000000001);
          intCents = dblValue%100;
          strCents = intCents.toString();
          dblValue = Math.floor(dblValue/100).toString();
                strCents = "0" + strCents;
          for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
                dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
          return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);

    function unformatCurrency(strValue)
      strValue = strValue.replace("$","").replace(",","");
      return parseFloat(strValue);

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now