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.
I am looking for something for displaying distances with some perhaps
rough control of significant digits displayed. I'm thinking in term
of things like picture sizes, furniture sizes, distances to drive,
paper sizes where I convert back and forth between English and metric
measure.
Is there anything built-in I have overlooked?

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
>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.
Stefan Ram - 25 Apr 2008 03:30 GMT
>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.
Now, I have found it:
»After rounding for the precision, the formatting of the
resulting magnitude m depends on its value.
If m is greater than or equal to 10^-4 but less than
10^precision then it is represented in decimal format.
If m is less than 10^-4 or greater than or equal to
10^precision, then it is represented in computerized
scientific notation.
The total number of significant digits in m is equal to
the precision. If the precision is not specified, then the
default value is 6. If the precision is 0, then it is
taken to be 1.«
http://download.java.net/jdk7/docs/api/java/util/Formatter.html
Roedy Green - 25 Apr 2008 11:11 GMT
> »After rounding for the precision, the formatting of the
> resulting magnitude m depends on its value.
[quoted text clipped - 9 lines]
> the precision. If the precision is not specified, then the
> default value is 6. If the pr
here are some sample uses
// Use of G format:
// %[flags][min width of field].[number of significant digits of
precision (not # of decimal places)]g
System.out.printf( "value is %4.3g\n", 1.4d );
// prints value is 1.40
System.out.printf( "value is %4.3g\n", 1.456d );
// prints value is 1.46
System.out.printf( "value is %4.3g\n", 145.0d );
// prints value is _145
System.out.printf( "value is %8.4g\n", 1.5d );
// prints value is ___1.500
System.out.printf( "value is %4.4g\n", 1.5E-6 );
// prints value is 1.500e-06
System.out.printf( "value is %,8.6g\n", 12345.0d );
// prints value is 12,345.0
I congratulate whomever designed it. It is considerably more useful
than the old FORTRAN G format it derives from.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com