| 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%
|
 |
 |
|
|
subject: After Locale I read NumberFormat and noticed :
|
|
|