%f:格式化一个浮点数。
%g:使代码使用科学记数法。
我在测试下面这段代码时发现如下一个现象,应该如何理解比较容易记忆呢??谢谢。
float i = 3f;
System.out.printf("5Int: %5g ",i);
输出为://科学记数法。(5个0)
5Float: 3.00000当为:
System.out.printf("5Int: %5f ",i);
输出为://格式化一个浮点数。(6个0)
5Float: 3.000000一个是5个0,一个是6个0。我只是想请教大家这个区别如何理解比较好记呢?谢谢
丫滴,我都开始想我自己已经钻了牛角尖了!!!
http://java.sun.com/javase/6/docs/api/java/util/Formatter.html关于这些占位符都有解释。
For the floating-point conversions 'e', 'E', and 'f' the precision is the number of digits after the decimal separator. If the conversion is 'g' or 'G', then the precision is the total number of digits in the resulting magnitude after rounding.
用f的话是小数点后5位,用g就是总共5位。
float i = 3f;
System.out.printf("5Int: %.6g ",i);
System.out.printf("5Int: %.5f ",i);
这样就都是5个0了