开发者

How to always show decimal part when using DecimalFormat?

Float sum = new Float(300); // always somehow calculated
DecimalFormat df = new DecimalFormat("#.#");
String s = df.format(sum/3);  // prints 100, I want 100.0
s = df.format(301/3); // pritns 100.3 which is 开发者_C百科correct

Result should always be formatted to 1 decimal palce, how to do so?


Change

DecimalFormat df = new DecimalFormat("#.#");

to

DecimalFormat df = new DecimalFormat("#.0");

Basically, a 0 means "always show the digit in this position", where a # means "show the digit in this position unless it's zero".


You can read about the patterns here. Change the following line to should do the trick.

DecimalFormat df = new DecimalFormat("#.0");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜