Stop DecimalFormat with percentage sign from moving the decimal

ryvantage picture ryvantage · Jan 14, 2014 · Viewed 8.1k times · Source

Is there a way to prevent a DecimalFormat object from automatically moving the decimal place two places to the right?

This code:

double d = 65.87;
DecimalFormat df1 = new DecimalFormat(" #,##0.00");
DecimalFormat df2 = new DecimalFormat(" #,##0.00 %");
System.out.println(df1.format(d));
System.out.println(df2.format(d));

produces:

65.87
6,587.00 %

But I'd like it to produce:

65.87
65.87 %

Answer

PopoFibo picture PopoFibo · Jan 14, 2014

Surround your % with single quotes:

DecimalFormat df2 = new DecimalFormat(" #,##0.00 '%'");