Printing Floating-Point Numbers

A floating-point value contains a decimal point, as in 33.5, 0.0 or -657.983. Floating-point values are displayed in one of several formats. Figure 28.3 describes the floating-point conversions. The conversion character e and E displays floating-point values in computerized scientific notation (also called exponential notation). Exponential notation is the computer equivalent of the scientific notation used in mathematics. For example, the value 150.4582 is represented in scientific notation in mathematics as

1.504582 x 102

Figure 28.3. Floating-point conversion characters.

(This item is displayed on page 1330 in the print version)

Conversion character

Description

e or E

Display a floating-point value in exponential notation. When conversion character E is used, the output is displayed in uppercase letters.

f

Display a floating-point value in decimal format.

g or G

Display a floating-point value in either the floating-point format f or the exponential format e based on the magnitude of the value. If the magnitude is less than 10 3, or greater than or equal to 10 7, the floating-point value is printed with e (or E). Otherwise, the value is printed in format f. When conversion character G is used, the output is displayed in uppercase letters.

a or A

Display a floating-point number in hexadecimal format. When conversion character A is used, the output is displayed in uppercase letters.

and is represented in exponential notation as

1.504582e+02

in Java. This notation indicates that 1.504582 is multiplied by 10 raised to the second power (e+02). The e stands for "exponent."

Values printed with the conversion characters e, E and f are output with six digits of precision to the right of the decimal point by default (e.g., 1.045921)other precisions must be specified explicitly. For values printed with the conversion character g, the precision represents the total number of digits displayed, excluding the exponent. The default is six digits (e.g., 12345678.9 is displayed as 1.23457e+07). Conversion character f always prints at least one digit to the left of the decimal point. Conversion character e and E print lowercase e and uppercase E preceding the exponent and always print exactly one digit to the left of the decimal point. Rounding occurs if the value being formatted has more significant digits than the precision.

Conversion character g (or G) prints in either e (E) or f format, depending on the floating-point value. For example, the values 0.0000875, 87500000.0, 8.75, 87.50 and 875.0 are printed as 8.750000e-05, 8.750000e+07, 8.750000, 87.500000 and 875.000000 with the conversion character g. The value 0.0000875 uses e notation because the magnitude is less than 10-3. The value 87500000.0 uses e notation because the magnitude is greater than 107. Figure 28.4 demonstrates each of the floating-point conversion characters.

Figure 28.4. Using floating-point conversion characters.

(This item is displayed on page 1331 in the print version)

1 // Fig. 28.4: FloatingNumberTest.java 2 // Using floating-point conversion characters. 3 4 public class FloatingNumberTest 5 { 6 public static void main( String args[] ) 7 { 8 System.out.printf( "%e ", 12345678.9 ); 9 System.out.printf( "%e ", +12345678.9 ); 10 System.out.printf( "%e ", -12345678.9 ); 11 System.out.printf( "%E ", 12345678.9 ); 12 System.out.printf( "%f ", 12345678.9 ); 13 System.out.printf( "%g ", 12345678.9 ); 14 System.out.printf( "%G ", 12345678.9 ); 15 } // end main 16 } // end class FloatingNumberTest  

1.234568e+07 1.234568e+07 -1.234568e+07 1.234568E+07 12345678.900000 1.23457e+07 1.23457E+07  

Категории