Java
  Home arrow Java arrow Page 5 - Formatters and Java Print Streams
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Formatters and Java Print Streams
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2007-06-21

    Table of Contents:
  • Formatters and Java Print Streams
  • Character Sets
  • Locales
  • Format Specifiers
  • Floating-point conversions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    %f
       A regular base-10 decimal number, such as 3.141593

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

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

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

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

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

    %A
       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.

       · This article is an excerpt from the book "Java I/O, Second Edition," published by...
     

    Buy this book now. This article is excerpted from chapter seven of Java I/O, Second Edition, written by Elliotte Rusty Harold (O'Reilly, 2006; ISBN: 0596527500). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT