how to reformat the currency?

H

hoho123

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!
 
N

nblloyd

String num = new String("2,111,000.09").replaceAll(",", "");
double d = Double.parseDouble(num);

- Natasha -
 
C

Chris Smith

hoho123 said:

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).
 
R

Rhino

hoho123 said:
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));

}
 
G

Greg R. Broderick

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 (e-mail address removed)

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

nblloyd

hoho123 said:

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 -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top