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.
我试图在 Java 中使用 printf 截断到第三个小数点,但我一直只得到第二个小数点。这是我尝试这样做的代码行:
System.out.printf("The speed in ft/sec is %6.2f\n", time);
试试%6.3f吧。格式是
%6.3f
%(before).(after)(type) 6 3 f 6 -> 6 digits before the decimal 3 -> 3 digits AFTER the decimal
试试这个:
System.out.printf("The speed in ft/sec is %6.3f\n", time);
以上将四舍五入到小数点后三位(而不是“截断”它们)。唯一的区别在于点之后的值。