DecimalFormat vs String.format

时间:2018-03-23 00:24:51

标签: java decimalformat

Am I missing something here? According to the doc, String.format "f" use HALF_UP rounding algorithm. So shouldn't the following two print the same result?

DecimalFormat df = new DecimalFormat("#0.0");
double num = 3.55;
df.setRoundingMode(RoundingMode.HALF_UP);
System.out.println(df.format(num));             // prints "3.5"
System.out.println(String.format("%.1f", num)); // prints "3.6"

0 个答案:

没有答案
相关问题