Tuesday, May 10, 2011

Program to format the decimal display in Java

I need a generalized format (00.00) to display the parameter ‘Speed’ in Java.
For eg:- If the speed is 10.5, I need it to be displayed in the format ‘10.50’.

The DecimalFormat Class can be used to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator.

import java.text.DecimalFormat;

public class doubleFormat
{
 public static void main(String[] args)
 {
  DecimalFormat dfSpeedFormatter = new DecimalFormat("00.00");
  String strSpeed;
  double dSpeed = 0.5;
  strSpeed = dfSpeedFormatter.format(dSpeed);
  System.out.println(strSpeed);
 }
}

The output will be displayed as:
00.50



Source:- http://download.oracle.com/javase/tutorial/java/data/numberformat.html

No comments:

Post a Comment

Thanks for visiting my Blog....Ur comments n suggestions are most valuable for me...Thanks