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));
}