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
 

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net