Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: Fortran-lik...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 4 Topic 16019 of 16085
Post > Topic >>

Re: Fortran-like number formatting

by ram@[EMAIL PROTECTED] (Stefan Ram) Apr 25, 2008 at 01:45 AM

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.




 4 Posts in Topic:
Fortran-like number formatting
Roedy Green <see_websi  2008-04-25 01:14:30 
Re: Fortran-like number formatting
ram@[EMAIL PROTECTED] (S  2008-04-25 01:45:22 
Re: Fortran-like number formatting
ram@[EMAIL PROTECTED] (S  2008-04-25 02:30:39 
Re: Fortran-like number formatting
Roedy Green <see_websi  2008-04-25 10:11:29 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat May 17 6:24:48 CDT 2008.