473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Currency Conversion

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
6 1680
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


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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: techy techno | last post by:
hii Experts..!! I need someone to tell me where I can get a Currency Converter like http://cconv.textor.com please can someone tell me where I can get it I need it for free + I dont need...
2
by: Willing 2 Learn | last post by:
I'm still having trouble getting my program to do arithmetic in cents(keeping all #'s) then convert the answer in a format of dollars & cents. The main program should add, subtract, scalar...
3
by: Josh | last post by:
I am writing a program where the user inputs currency in US dollars. I want the program to only accept valid currency input, converting the string into the proper type of variable (double?), and...
11
by: Adrian | last post by:
Hi I want to use the following declarations but vb dotnet keeps complaining that currency can't be used because it private ? I have tried it in a module and in the declaration pare same error!...
7
by: meenasamy | last post by:
Hi all, i need to create a function that takes three parameters( Original currency, needed currency, amount) i need to convert from the original currency to the needed currency the amount and...
2
by: sck10 | last post by:
Hello, I have a SQL Server 2K table with a field set to currency. When I try to insert or update a FormView, I get the following error: Disallowed implicit conversion from data type nvarchar...
10
by: jayender.vs | last post by:
Hello guys, I need to know the Currency conversion code in Javascript Say for example i got 2 textbox .. where i enter a number (singapore doller value) and i provide a button and in the next text...
18
by: Boris Yeltsin | last post by:
OK, I have a database table, it has prices of products in it, like so: ProductPrice MONEY ProductIsoCurrencyCode CHAR(3) Now, both CultureInfo and RegionInfo have...
16
by: xjohnx | last post by:
Hi, I'm hoping someone can help me I am quite new to Javascript and have had to create a programme which converts dollars into euros and vice versa, here is my script which is working var...
25
by: mereba | last post by:
Hello My country Ghana is changing its currency. I want to write a small programme in C++ that can covert from the old currency into the new one. I would like this programme to run behind a simple...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.