Show padding zeros using DecimalFormat

2019-01-11 09:04发布

I'm using DecimalFormat to format doubles to 2 decimal places like this:

DecimalFormat dec = new DecimalFormat("#.##");
double rawPercent = ( (double)(count.getCount().intValue()) / 
                            (double)(total.intValue()) ) * 100.00;
double percentage = Double.valueOf(dec.format(rawPercent));

It works, but if i have a number like 20, it gives me this:

20.0

and I want this:

20.00

Any suggestions?

7条回答
Animai°情兽
2楼-- · 2019-01-11 10:10

Try using a DecimalFormat of "0.00" instead. According to the JavaDocs, this won't strip off the extra 0s.

查看更多
登录 后发表回答