This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting decimalFormatSymbols

 
J E Hansen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Pie
Posts: 20763
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic