Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想像这样格式化一个十进制数:
1234.5 0.5
这意味着最大数字为 9999.9(“.”前 4 个字符,后 1 个字符)
使用DecimalFormat:
DecimalFormat
DecimalFormat df = new DecimalFormat("###0.0"); String str = df.format(yourDecimal);
如果要打印结果,使用 printf 可能更简单
for (double d : new double[]{1234.51111, 0.501}) System.out.printf("%6.1f%n", d);
印刷