Xor*_*rty 10 java 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 correct
Run Code Online (Sandbox Code Playgroud)
结果应始终格式化为1十进制,如何操作?
Gar*_*ryF 35
更改
DecimalFormat df = new DecimalFormat("#.#");
Run Code Online (Sandbox Code Playgroud)
至
DecimalFormat df = new DecimalFormat("#.0");
Run Code Online (Sandbox Code Playgroud)
基本上,0表示"始终在该位置显示数字",其中#表示"在该位置显示数字,除非它为零".
| 归档时间: |
|
| 查看次数: |
18973 次 |
| 最近记录: |