如何在Java中使用printf截断?

Cha*_*gne 2 java format

我试图在Java中使用printf截断到第三个小数点,但我一直只到第二个小数点.这是我试图这样做的代码行:

System.out.printf("The speed in ft/sec is %6.2f\n", time);
Run Code Online (Sandbox Code Playgroud)

Mar*_*c B 7

试试吧%6.3f.格式是

%(before).(after)(type)
    6         3    f

    6 -> 6 digits before the decimal
    3 -> 3 digits AFTER the decimal
Run Code Online (Sandbox Code Playgroud)


Ósc*_*pez 5

试试这个:

System.out.printf("The speed in ft/sec is %6.3f\n", time);
Run Code Online (Sandbox Code Playgroud)

以上将舍入到三位小数(不是"截断"它们).唯一的区别在于点后的值.