| Author |
BigDecimal Contsructer Pitfall.
|
David Garratt
Ranch Hand
Joined: Aug 08, 2003
Posts: 184
|
|
In my code I am creating a BigDecimal by passing it a string as part of the contructor :-
BigDecimal mynumber = new BigDecimal("12345.67");
That works fine except when the string is not using the local regional settings as me and I try and do something like :-
BigDecimal mynumber = new BigDecimal("12.345,67");
So what would be the clever way of handling this ? Do I change the string variable to the "standard" format before I cast it to BigDecimal or is there a better way ?
Thanks in advance.
Dave
|
 |
David Garratt
Ranch Hand
Joined: Aug 08, 2003
Posts: 184
|
|
Just to clarify - what I need is a way to convert a number held in a string into a big decimal which can handle the number within the string being formatted according to any locale.
I have tried to avoid putting a decimal into a BigDecimal constructor as it seems to return unpredictable results.
Something along
public BigDecimal convertStringtoBigDecimal(String localeformattednumber)
{
BigDecimal result = new BigDecimal("0");
something clever...
return result;
}
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
I did a bit of searching, and it looks like java.text.DecimalFormat might do what you want. in particular, it's got a setParseBigDecimal method, and once that's set to true then the parse method returns BigDecimals.
You can usually get a locale-specific DecimalFormat using NumberFormat.getInstance, and casting it to a DecimalFormat. According to the Javadocs: "This will work for the vast majority of locales; just remember to put it in a try block in case you encounter an unusual one. "
|
 |
David Garratt
Ranch Hand
Joined: Aug 08, 2003
Posts: 184
|
|
Thanks for this, I've been experimenting but just getting myself in a mess.
What I don't understand is why the following code when run on an Italian machine generates an exception for the following line of code
Double temp = Double.valueOf(localeQuantity);
where localeQuantity is a String containing a valid Italian representation of a number like '123.456,000'
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2782
|
|
David Garratt wrote:What I don't understand is why the following code when run on an Italian machine generates an exception for the following line of code
Double temp = Double.valueOf(localeQuantity);
If you look at the API for java.lang.Double, valueOf(String) isn't designed to use any localized format. It uses a standard format that will be the same on any machine anywhere in the world. Which can be very convenient for certain applications, but isn't what you want. The same API suggests if you need to use a localized format, use NumberFormat instead. Double.parseDouble() simply isn't designed for what you want to do.
|
 |
David Garratt
Ranch Hand
Joined: Aug 08, 2003
Posts: 184
|
|
That was most helpful. I think I have it all working now.
Many thanks
Dave
|
 |
 |
|
|
subject: BigDecimal Contsructer Pitfall.
|
|
|