这里首先介绍如何使用DecimalFormat,给出一段代码如下:
[code]double d = Math.PI;
String pattern = "###.###";
DecimalFormat formatter = new DecimalFormat(pattern);
String hitRate = formatter.format(d);
assertTrue("3.142".equals(hitRate));[/code]
这里需要注意的是“四舍五入”
[code]double d = Math.PI;
String pattern = "###.###";
DecimalFormat formatter = new DecimalFormat(pattern);
String hitRate = formatter.format(d);
assertTrue("3.142".equals(hitRate));[/code]
这里需要注意的是“四舍五入”
本文介绍了如何使用Java中的DecimalFormat类来格式化数字,并提供了一个具体的示例代码。通过设置特定的模式,可以实现对浮点数进行精确地格式化输出。

3510

被折叠的 条评论
为什么被折叠?



