使用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
结果应始终格式化为小数点后 1 位,如何做到这一点?
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
Result should always be formatted to 1 decimal palce, how to do so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改
为
基本上,0 表示“始终在该位置显示数字”,其中 # 表示“在该位置显示数字,除非它为零”。
Change
to
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.