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 ]
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
Joined: Jun 28, 2007
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..
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
Joined: Jun 28, 2007
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".