views:

496

answers:

2

I have a series of texfields, which I'd like to format as currency. Preferably, this would be done on the fly but at least onblur. What I mean by currency format is 349507 -> $349,507. Is it possible?

I prefer HTML/CSS/JS solutions, because I need less explanation. I'm not familiar at all with jQuery.

Any help is greatly appreciated.
Mike

+4  A: 
John Kugelman
Thank you, John. I used this method in application to get on the fly formatting for a currency field. Very easy to read too! +1
Jason Slocomb
-1 It has a bug and does not work with numbers ending in .9999, eg "1879.9999" formats as 1,879.10 (!)
JK
+3  A: 

First result in a Google search for "javascript format currency"

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

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;
}
Kane Wallmann