Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

formating and storing monetary values

Status
Not open for further replies.

msloan

Programmer
May 30, 2001
39
US
Just looking for some ideas on what the best way to store and format currency values might be.

I am using SQL 7.0 and have my currency fields defined as float types. In my java, I call and store the value as a float (getFloat() updateFloat()).

What I am finding is when I convert from US dollars to say Turkish Lira, my values either get truncated or formatted into exponential notation. This becomes more of a problem when dealing with larger values (say $5,345,234.99).

I have been toying with the idea of storing everything in US$ and when calling from the database, format the value using java.util.Currency into the correct locale, but I wanted to see if anyone else had a better suggestion.

TIA
 
i think storing them as floats is fine. if you're having problems with formating then perhaps you should (on displaying your result) convert to a string, then truncate after the second decimal place.

float money = 23.56785;

String s = Float.toString(money);
Int n = s.latIndexOf('.');
String finalMoney = s.substring(0, n+2);
 
>> format the value using java.util.Currency into the correct locale

That would be correct. However, this will not calculate exchange rates, it will only provide locale formatting of the value.

To perform currency exchange rate calculations you would need to provide that yourself. Since exchange rates change all the time I would advise not incurring that overhead if it is not a requirement for the application.


-pete
 
i wonder if there is there a reliable, uptodate and online source that you can get currency conversions from through java code.
 
If there is an online source, and you would think there would be, then you could access it from Java of course. Keep in mind it is probably not free.

-pete
 
well free is as free does. all you have to do is find a website that does currency conversions (like yahoo) and exploit them.
 
Yes, actually a Google for "currency exchange rates" online returned almost 40,000 hits [lol]

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top