• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

After Locale I read NumberFormat and noticed :

 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// But 12% is printed when
double d = 12.34 / 100.0 ; // Wrong should be 12.34%
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic