Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Is there an easy way to format currency in Javascript?

Posted on 2012-04-06
6
Medium Priority
?
269 Views
Last Modified: 2012-04-15
Hi,

Is there an easy way to format currency in Javascript?

Ex.  10000.00  to $10,000.00.


Thanks
0
Comment
Question by:taborrg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 17

Assisted Solution

by:Anuroopsundd
Anuroopsundd earned 1000 total points
ID: 37818467
function CurrencyFormatted(amount)
{
      var i = parseFloat(amount);
      if(isNaN(i)) { i = 0.00; }
      var minus = '';
      if(i < 0) { minus = '-'; }
      i = Math.abs(i);
      i = parseInt((i + .005) * 100);
      i = i / 100;
      s = new String(i);
      if(s.indexOf('.') < 0) { s += '.00'; }
      if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
      s = minus + s;
      return s;
}

http://www.web-source.net/web_development/currency_formatting.htm


http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 1000 total points
ID: 37818473
// Slightly less complex

function currency(amount) {
    var pennies = parseInt(amount * 100);
    var reverse = pennies.toString().split("").reverse().join("");
    reverse = reverse.replace(/(\d{2})/, "$1.");
    reverse = reverse.replace(/(\d{3})/g, "$1,");
    return reverse.split("").reverse().join("").replace(/^,*/, "$$");
}

alert(currency(123456.78))
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 37818480
//Negative amounts

function currency(amount) {
    var prefix = ( amount < 0 )? "-" : "";
    var pennies = parseInt(Math.abs(amount) * 100);
    var reverse = pennies.toString().split("").reverse().join("");
    reverse = reverse.replace(/(\d{2})/, "$1.");
    reverse = reverse.replace(/(\d{3})/g, "$1,");
    var result = reverse.split("").reverse().join("").replace(/^,*/, "$$");
    return prefix + result;
}

alert(currency(-123456.78))
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 1

Author Comment

by:taborrg
ID: 37818489
Thanks!

I'm going to have to work on this a bit later as it turns out.
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 37819080
Do you have to display it in a text box as soon as the user blurs it and/or while he types in it, or what else...?
0
 
LVL 1

Author Closing Comment

by:taborrg
ID: 37849621
Thanks for the help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

705 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