Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

how to reformat the currency?

Thread view: 
hoho123 - 30 Jun 2006 22:00 GMT
I have a currency string like 2,111,000.09
what's the easiest way to change it back to a double type?

Thank you!
nblloyd@gmail.com - 30 Jun 2006 22:08 GMT
String num = new String("2,111,000.09").replaceAll(",", "");
double d = Double.parseDouble(num);

- Natasha -

> I have a currency string like 2,111,000.09
> what's the easiest way to change it back to a double type?
>
> Thank you!
hoho123 - 30 Jun 2006 22:09 GMT
thx
> String num = new String("2,111,000.09").replaceAll(",", "");
> double d = Double.parseDouble(num);
[quoted text clipped - 5 lines]
> >
> > Thank you!
Chris Smith - 01 Jul 2006 00:09 GMT
> > String num = new String("2,111,000.09").replaceAll(",", "");
> > double d = Double.parseDouble(num);

> thx

Wait!  Danger, Will Robinson.  This is a really bad idea.  If you've got
a number formatted in some locale-specific way (such as the example with
commas), then writing code like the above to convert it to a double is
broken.  It will work in some locales and fail in others.  Instead, use
NumberFormat.getCurrencyInstance().parse to parse the value in a way
that's appropriate for the current Locale (or, if the number came from a
different locale, use the version of getCurrencyInstance that takes a
Locale object as a parameter).

Signature

Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation

nblloyd@gmail.com - 03 Jul 2006 15:06 GMT
> > > String num = new String("2,111,000.09").replaceAll(",", "");
> > > double d = Double.parseDouble(num);
>
> > thx
>
> Wait!  Danger, Will Robinson.  This is a really bad idea. [...]

Good point. I didn't think through the problem when I posted. So,
ignore my response and listen to the other people who have posted here.
:)

- Natasha -
Vova Reznik - 30 Jun 2006 22:10 GMT
> I have a currency string like 2,111,000.09
> what's the easiest way to change it back to a double type?
>
> Thank you!

Use NumberFormat.
Rhino - 01 Jul 2006 05:26 GMT
>I have a currency string like 2,111,000.09
> what's the easiest way to change it back to a double type?

This example illustrates the code you should use if you want the currency
formatting to be sensitive to whatever Locale is in effect. I'm assuming you
know what a Locale is but if you don't, consult this chapter in the Java
Tutorial: http://java.sun.com/docs/books/tutorial/i18n/index.html.

EXPLANATION
The example uses three arbitrary Locales just to show you how different the
result can be, depending on which Locale is in effect. In your code, you
will probably have the program use the default Locale, which will typically
be set differently for users in different regions.

The example is just a simple loop that executes once for each of the Locales
in the array of Locales. For each element in the array of locales, the code
displays the value of the locale as a String. The three locales in the
example are de_DE (German-speaking regions of Germany), fr_CH
(French-speaking regions of Switzerland), and en_US (English-speaking
regions of the United States).

Once the Locale has been displayed, the NumberFormat class is used to create
an instance of a currency amount called currencyFormat. The NumberFormat can
be reused as many times as desired.

Then the example explores two assumptions. First, it is assumed that the
number which needs to be formatted is stored in a double. Second, it is
assumed that the number which needs to be formatted is stored in a String.

In the first case, the format() method of NumberFormat can be applied
directly to the double, which is called myAmount. Then, the formatted amount
is displayed.

In the second case, the String version of the number has to first be
converted to a double. This can probably be done more concisely to reduce
the number of Objects created but I'm just doing it this way so that you can
see all the steps. The String is converted to a Double, then the Double is
converted to a double. Then the new double version of the value has the
format() method of NumberFormat applied to it and is displayed.

In short, you can format your currency amount in a way that is sensitive to
your user's Locale regardless of whether the amount is stored in a double or
a String.

Here's the code:

/* Format a number as a locale-sensitive currency. */

Locale[] locales = {new Locale("de", "DE"), new Locale("fr", "CH"), new
Locale("en", "US")};

for (Locale oneLocale : locales) {

   /* Display the value of the locale which is being used in this iteration
of the loop. */

   System.out.println("Locale: " + oneLocale.toString());

   /* Create a formatter for a currency amount that is sensitive to the
specified format. */

   NumberFormat currencyFormat =
NumberFormat.getCurrencyInstance(oneLocale);

   /*

   * The number is already stored in a double so display it formatted
according to the

   * rules for the specified locale.

   */

   double myAmount = 2111000.09;

   System.out.println(" myAmount: " + currencyFormat.format(myAmount));

   /*

   * The number is expressed as a String. Convert it to a Double and then
to a double, then

   * display it so that it is formatted according to the rules for the
specified format.

   */

   String myAmount2 = "2111000.09";

   Double myAmount2Double = Double.valueOf(myAmount2);

   double myAmount2double = myAmount2Double.doubleValue();

   System.out.println(" myAmount2: " +
currencyFormat.format(myAmount2double));

}

--

Rhino
Greg R. Broderick - 01 Jul 2006 16:38 GMT
> I have a currency string like 2,111,000.09 what's the easiest way to
> change it back to a double type?

No you don't!  A double suffers from floating point rounding imprecision.  
Use something like java.math.BigDecimal if you want to prevent rounding
errors.  If performance is critical, consider using a long integer to
represent the currency value in cents (assuming US currency).

Reference:
<http://www.google.com/search?q=currency+floating+point+rounding&start=0>

Cheers
GRB

-- ---------------------------------------------------------------------
Greg R. Broderick            gregb.usenet200606@blackholio.dyndns.org

A. Top posters. Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.