This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes After Locale I read NumberFormat and noticed : Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "After Locale I read NumberFormat and noticed :" Watch "After Locale I read NumberFormat and noticed :" New topic
Author

After Locale I read NumberFormat and noticed :

Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
NumberFormat nf = NumberFormat.getPercentInstance() ;
String s = nf.format(12) ;
System.out.println(s) ; // 1200%
// No arithmetic will fix this - I want to keep the %.

nf = NumberFormat.getInstance() ;
float f = 123.45678f ;
System.out.println (fractionNF.getMaximumFractionDigits)) ; // 3 - default?
System.out.println (fractionNF.format(f)) ; // 123.457
// Rounds to nearest decimal point. Why ?

S&B page 470 write "This, on our JVM produces..." and do not mention percent.
Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
Sometimes the obvious solution is missed :

NumberFormat nf = NumberFormat.getPercentInstance() ;
double d = 12 / 100.0 ; // Divide BEFORE format()
String s = nf.format(d) ;
System.out.println(s) ; // 12%
Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
// But 12% is printed when
double d = 12.34 / 100.0 ; // Wrong should be 12.34%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: After Locale I read NumberFormat and noticed :