JDK 1.5的printf的bug?
狐帝 2004-05-07 06:37:44 请看下面的代码:
import java.io.*;
import java.lang.*;
public class TJava5Printf
{
public TJava5Printf()
{
}
public void run()
{
System.out.printf( "Output 0.001 by %%f is: %f\n", 0.001 ); // 打印0.1000 !
System.out.printf( "Output 1.0 by %%f is: %f\n", 1.0 ); // 打印1.000
System.out.printf( "Output 0.001 by %%e is: %e\n", 0.001 ); // 打印1.000e-3
}
public static void main( String args[] )
{
TJava5Printf testObj = new TJava5Printf();
testObj.run();
}
}
当用%f格式输出0.001这样的小于1.0的浮点数时,有效数字前面的0全被吃掉了!也就是说,用%f输出0.001,屏幕上其实会显示0.1。这是否是printf处理%f的bug?