aspose file tools*
The moose likes Java in General and the fly likes BigDecimal Contsructer Pitfall. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "BigDecimal Contsructer Pitfall." Watch "BigDecimal Contsructer Pitfall." New topic
Author

BigDecimal Contsructer Pitfall.

David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 196

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: 196

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: 4384
    
    8

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: 196

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: 3014
    
  10
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: 196

That was most helpful. I think I have it all working now.

Many thanks

Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BigDecimal Contsructer Pitfall.