%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。我只是想请教大家这个区别如何理解比较好记呢?谢谢
丫滴,我都开始想我自己已经钻了牛角尖了!!!

解决方案 »

  1.   

    为什么java中这么多有用的知识点楼主不去研究,反而在乎这种莫名其妙的问题呢?
      

  2.   

    Java 这种格式化输出使用的都是一个叫 Formatter 的类:
    http://java.sun.com/javase/6/docs/api/java/util/Formatter.html关于这些占位符都有解释。
      

  3.   

    jdk的文档解释的很清楚啊
    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位。
      

  4.   

    再说你的格式字符串也不对,你格式是整形部分
    float i = 3f;
    System.out.printf("5Int: %.6g ",i);
    System.out.printf("5Int: %.5f ",i);
    这样就都是5个0了