Java Data Types - Numeric Data
Written by Alex Armstrong   
Wednesday, 06 February 2013
Article Index
Java Data Types - Numeric Data
Literals and Expressions
Types of Assignment and Casting
Numeric Output

Numeric Output

Before a number can be displayed it has to be converted to a string - more on strings in the next chapter.

The simplest way to get numeric output is to use the println or print methods of out

double myDouble=1.523454565;
int myInt=2;
System.out.println(myInt);
System.out.println(myDouble);

 In each case the printlin method performs a default conversion of the numeric value to a string.

What if we want to control the conversion?

The answer is that you can use the format or printf methods in place of print or println.

The way that this works is that you specify a format and the variables to be printed. The rules for the format can be complicated but the first rule is that any regular character in the format is just displayed. So for example:

System.out.printf("this is my number",myDouble);

just displays "this is my number" the value in myDouble isn't displayed.

To get a the value in a variable displayed you have to include a format specifier which all start with %. How the number is displayed depends on the format specifier you choose.

For example:

System.out.printf(
        "this is my number %f",myDouble);

displays:

this is my number 1.523455

Notice that the value of the variable is displayed where the format specifier occurs - the value replaces the format specifier in the output.

If you want to specify the number total number of characters and the digits after the decimal point to use then you can precede the f  by two numbers c.d which gives the total number of characters including the decimal point and fractional digits f, For example:

System.out.printf(
         "this is my number %5.2f",myDouble);

displays the floating point number using a total of 5 characters with 2 digits after the decimal point i.e.

this is my number  1.5

You can use other format specifiers for other numeric types and for general formatting:

  • %d is a decimal integer
  • %f is a floating point value
  • %n is a newline

If you add a leading + you get a sign printed, a comma includes a locale specific grouping character and a - left justified the value in the space.

System.out.printf(
   "this is my number %+-10.2f",myDouble);

produces

this is my number +1.52

There are a lot more ways to format numbers and you should lookup the DecimalFormat class if you want to control the way values are displayed even more.

 

Modern Java Contents

  1. Getting started with Java
  2. Introducing Java - Swing Objects
  3. Writing Java Code - Methods
  4. Command Line Programs
  5. The Java User Interface - More Swing
  6. Working With Class
  7. Class Inheritance
  8. Building a Java GUI - Containers

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin,  or sign up for our weekly newsletter.

blog comments powered by Disqus



Last Updated ( Wednesday, 06 February 2013 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.