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

Setting decimalFormatSymbols

J E Hansen
Greenhorn

Joined: Jun 28, 2007
Posts: 3
Hi
I am having problems settingdecimalFormatSymbols such as the decimal separator. The API states:

This class represents the set of symbols (such as the decimal separator, the grouping separator, and so on) needed by DecimalFormat to format numbers. DecimalFormat creates for itself an instance of DecimalFormatSymbols from its locale data. If you need to change any of these symbols, you can get the DecimalFormatSymbols object from your DecimalFormat and modify it.


In my code:


I expect separator to be ":", but no change...
[ April 14, 2008: Message edited by: J E Hansen ]
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

How are you using your DecimalFormatSymbols once they are set. The following code results in using the colon ":" as the decimal separator:



You can check out The Java Tutorials (where I started for this example) for more details


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You don't show the code where you proceeded to use that NumberFormat object to format something. You did do that, right? And did you check to make sure that you actually called setDecimalSeparator?
J E Hansen
Greenhorn

Joined: Jun 28, 2007
Posts: 3
If I want to change symbols, I need to create a new DecimalFormat for it to work. More intuitive would be if you only had to change the symbols using the current DecimalFormat's DecimalFormatSymbols (as I tried). This would be consistent with the general OO thinking.

Instead, one have to constantly keep a tab of all symbols, and reset all of these every time one needs to change one (and then instantiate a whole new DecimalFormat with these)...

The API explains this with the need for localizing NumberFormat, and thus reads the symbols from a resourcebundle. Thats fine, but if anyone want to specify a new symbol, that is only confusing. A bug in my view..

Do you agree?
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Originally posted by J E Hansen:
If I want to change symbols, I need to create a new DecimalFormat for it to work. More intuitive would be if you only had to change the symbols using the current DecimalFormat's DecimalFormatSymbols (as I tried). This would be consistent with the general OO thinking.


If you read the Javadoc for the constructor DecimalFormat(String,DecimalFormatSymbols), it states that's the constructor to use when you need to customize the format's behavior. Further, Javadoc for DecimalFormat.getDecimalFormatSymbols explicitly states that the DecimalFormatSymbols is rarely changed by the programmer or user.

By providing a constructor that lets you set the desired DecimalFormatSymbols, Java is providing you the tools you need to make the changes you need while making DecimalFormat as a whole as easily used for the vast majority of users (who generally just want the easiest way possible to have decimals formatted correctly for the desired Locale).

Some of Java's APIs can be a bit confusing at first, but it can be worthwhile to figure out why it's done the way it is.
J E Hansen
Greenhorn

Joined: Jun 28, 2007
Posts: 3
Creating a new DecimalFormat, pattern and DecimalFormatSymbols everytime you need to change a single symbol is cumbersome and non ObjectOriented, and I don't really understand wy they have made it this way. If anyone needs to change a symbol, they obviously do not care for any premade locale, and as such it should be unneccessary for DecimalFormat to lookup the symbols in the locale resourcebundle in these cases. Im not looking for a solution to change the symbols, I am just wondering why so cumbersome. The fact that the API says it is so, does not make it "sensible".
 
 
subject: Setting decimalFormatSymbols