Home arrow Java arrow Page 5 - Formatters and Java Print Streams

Formatters and Java Print Streams

Last week, we discussed Java print streams, concluding with the format method and formatter objects. This week, we pick up from where we left off. This is the second part of a three-part sereis. It is excerpted from chapter seven of Java I/O, Second Edition, written by Elliotte Rusty Harold (O'Reilly, 2006; ISBN: 0596527500). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 8
June 21, 2007
  1. · Formatters and Java Print Streams
  2. · Character Sets
  3. · Locales
  4. · Format Specifiers
  5. · Floating-point conversions

print this article

Formatters and Java Print Streams - Floating-point conversions
(Page 5 of 5 )

Floating-point conversions can be applied to all floating-point types: float and double, the type-wrapper classesFloatandDouble, andjava.math.BigDecimal. These conversions are:

   A regular base-10 decimal number, such as 3.141593

   A decimal number in scientific notation with a
   lowercase e, such as 3.141593e+00

   A decimal number in scientific notation with an
   uppercase E, such as 3.141593E+00

   A decimal number formatted in either regular or
   scientific notation, depending on its size and
   precision, with a lowercase e if scientific notation is

   A decimal number formatted in either regular or
   scientific notation, depending on its size and
   precision, with an uppercase E if scientific notation is

   A lowercase hexadecimal floating-point number, such
  as 0x1.921fb54442d18p1

   An uppercase hexadecimal floating-point number,
   such as 0X1.921FB54442D18P1

Surprisingly, you cannot use these conversions on integer types such asintorBigDecimal. Java will not automatically promote the integer type to a floating-point type when formatting. If you try to use them, it throws anIllegalFormatConversionException.

Example 7-2 prints π in all of these formats.

Example 7-2.  Floating-point format specifiers

public class FloatingPointFormatExample {

  public static void main(String[] args){
    System.out.printf("Decimal:      %f\n", Math.PI);
    System.out.printf("Scientific notation: %e\n", Math.PI);
    System.out.printf("Scientific notation: %E\n", Math.PI);
    System.out.printf("Decimal/Scientific: %g\n", Math.PI);
    System.out.printf("Decimal/Scientific: %G\n", Math.PI);
    System.out.printf("Lowercase Hexadecimal: %a\n", Math.PI);
    System.out.printf("Uppercase Hexadecimal: %A\n", Math.PI);

Here’s the output:

  Decimal:               3.141593
  Scientific notation:   3.141593e+00
  Scientific notation:   3.141593E+00
  Decimal/Scientific:    3.14159
  Decimal/Scientific:    3.14159
  Lowercase Hexadecimal: 0x1.921fb54442d18p1
  Uppercase Hexadecimal: 0X1.921FB54442D18P1

Please check back next week for the conclusion to this article.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials