Roedy Green <see_website@[EMAIL PROTECTED]
> writes:
>In the olden days Fortran had something called G format. It would
>display a number in minimum space, e.g. 7.6 but would revert to
>scientfic notation when it had to.
public class Main
{ public static void main( final java.lang.String[] args )
{ for( double v = java.lang.Math.PI * 1e4; v >= 1e-5; v /= 10 )
java.lang.System.out.printf( "%.2g%n", v ); }}
3,1e+04
3,1e+03
3,1e+02
31
3,1
0,31
0,031
0,0031
0,00031
3,1e-05
The documentation for »%g« on
http://download.java.net/jdk7/docs/api/java/util/Formatter.html#syntax
seems to be insufficient:
»'g', 'G'
floating point
The result is formatted using computerized scientific
notation or decimal format, depending on the precision and
the value after rounding.«
The documentation for »%g« in
ISO/IEC 9899:1999 (E)
is more detailed:
»g,G
A double argument representing a floating-point number is
converted in style f or e (or in style F or E in the case
of a G conversion specifier), with the precision
specifying the number of significant digits. If the
precision is zero, it is taken as 1.
The style used depends on the value converted; style e (or
E) is used only if the exponent resulting from such a
conversion is less than -4 or greater than or equal to the
precision.«
Either Sun Microsystems, Inc. expects the programmer to look
into ISO/IEC 9899:1999 (E) for »%g«, or there is a more
detailed description by Sun Microsystems, Inc., which I just
have not found yet.


|