2
我想在Java中使用printf截断到第三个小数点,但我一直只得到第二个小数点。下面是代码,我试图此行:如何在Java中使用printf截断?
System.out.printf("The speed in ft/sec is %6.2f\n", time);
我想在Java中使用printf截断到第三个小数点,但我一直只得到第二个小数点。下面是代码,我试图此行:如何在Java中使用printf截断?
System.out.printf("The speed in ft/sec is %6.2f\n", time);
试试这个:
System.out.printf("The speed in ft/sec is %6.3f\n", time);
上面会轮到小数点后三位(不是“截断”它们)。唯一的区别在于点之后的值。
改为尝试%6.3f
。格式是
%(before).(after)(type)
6 3 f
6 -> 6 digits before the decimal
3 -> 3 digits AFTER the decimal
你靠近,用'%6.3f' 看到这也 - http://stackoverflow.com/questions/7197078/printf-f-with-only-2-decimal-要点 – Coffee 2013-02-20 03:25:47
您可能只是在寻找System.out.printf(“速度英尺/秒是%.3f \ n”,时间); – 2013-02-20 03:26:01