go to previous page   go to home page   go to next page highlighting
DecimalFormat numform = new DecimalFormat("0.00"); 
System.out.println( "Num = " + numform.format(13.456) );

Answer:

Num = 13.46

Recall: all the digits of the whole-number part are output, regardless.

The fractional part of the format calls for two digits, so that is what is output.

Rounding happens automatically.


Padding with Zeros

import java.text.*;

public class IODemoZeroPadding
{
  public static void main ( String[] args )
  {
    DecimalFormat numform = new DecimalFormat("0000.0000"); 
    System.out.println( "Padding: " + numform.format( 23.15 ) );
  }
}

If there are more zeros in the pattern than needed, a number is padded with zeros on the left and right. For example, this program writes out (in the US)

Padding: 0023.1500

This can be useful for tables, where you wish to keep the decimal places aligned.


QUESTION 7:

What does the following fragment write?

DecimalFormat numform = new DecimalFormat("000.0"); 
System.out.println( "Num = " + numform.format(13.456) );

go to previous page   go to home page   go to next page