By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Currency Conversion

P: n/a
Hello,

Can someone tell me an easy way to convert a number with many trailing
digits to a currency format and adding the '$' sign? Thanks.

example

Convert 34.77389993 to $34.77

Thanks, News Guy
Oct 3 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 03/10/2005 18:55, News Guy wrote:
Can someone tell me an easy way to convert a number with many trailing
digits to a currency format and adding the '$' sign? Thanks.


/* n - Number to format (in pennies).
* c - Currency symbol to use (defaults to none).
* g - Thousands symbol (defaults to none).
* d - Decimal separator (defaults to dot [.]).
*
* Outputs a number of the form cngnnngnnn.nn
*
* For example, toCurrency(142635.7, '', ',') produces
* 1,426.36
*/
function toCurrency(n, c, g, d) {
var s = (0 > n) ? '-' : '';
var m = String(Math.round(Math.abs(n)));
var i = '', j, f; c = c || ''; g = g || ''; d = d || '.';

while(m.length < 3) {m = '0' + m;}
f = m.substring((j = m.length - 2));
while(j > 3) {
i = g + m.substring(j - 3, j) + i;
j -= 3;
}
i = m.substring(0, j) + i;
return s + c + i + d + f;
}

Take note of the arguments.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Oct 3 '05 #2

P: n/a


Simple:

var aNum=34.77389993; formattedNum='$'+aNum.toFixed(2);

the '$' has to be string, then just use .toFixed() to limit the floating.
Danny
Oct 4 '05 #3

P: n/a
Danny said the following on 10/2/2005 2:08 AM:

Simple:

var aNum=34.77389993; formattedNum='$'+aNum.toFixed(2);

the '$' has to be string, then just use .toFixed() to limit the floating.


And then worry about the bugs in .toFixed()

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 4 '05 #4

P: n/a
JRS: In article <sg***************@news01.roc.ny>, dated Mon, 3 Oct
2005 17:55:36, seen in news:comp.lang.javascript, News Guy
<ne**@yahoo.com> posted :
Can someone tell me an easy way to convert a number with many trailing
digits to a currency format and adding the '$' sign? Thanks.


Please read the newsgroup FAQ before asking about what is manifestly a
common need.

Numbers are binary, and don't have trailing digits; however, the default
conversion to String may give various non-zero numbers of digits after
the decimal point.

See <URL:http://www.merlyn.demon.co.uk/js-round.htm>

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 4 '05 #5

P: n/a
Thanks Danny, works great!

News Guy

"Danny" <da*******@bluebottle.com> wrote in message
news:1128398633.7c35f260384862cbc5fe9f4bf15e1b8b@t eranews...


Simple:

var aNum=34.77389993; formattedNum='$'+aNum.toFixed(2);

the '$' has to be string, then just use .toFixed() to limit the
floating.
Danny

Oct 4 '05 #6

P: n/a
News Guy wrote on 04 okt 2005 in comp.lang.javascript:
"Danny" <da*******@bluebottle.com> wrote in message
Simple:
var aNum=34.77389993; formattedNum='$'+aNum.toFixed(2);
Thanks Danny, works great!


Testing in IE6:

var aNum=0.0094; formattedNum='$'+aNum.toFixed(2);

// gives 0.00

var aNum=34.0094; formattedNum='$'+aNum.toFixed(2);

// gives 34.01

Not so great, eh?

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 4 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.