How to format number in Java?

How to format number in Java? In this tutorial you will learn about the code for formatting the number in Java.

How to format number in Java?

How to format number in Java? In this tutorial you will learn about the code for formatting the number in Java.

How to format number in Java?

Learn about the code of formatting the number in Java

This tutorial explains you how to format the number in Java. There are number of situations where we need the number to be formatted in many different formats. Depending the format required there are many ways to accomplish this task.

The recommended way of formatting the number is to use the DecimalFormat class. This class is most convenient class for formatting the number and provides ways of formatting the number such as leading zeros, prefixes, group separators etc... Here we are discussing about the DecimalFormat class for formatting the numbers.

About DecimalFormat class

The DecimalFormat class is a concrete subclass of NumberFormat class. It is used for the formatting of the numbers in many different formats and this class is almost sufficient for most of the programming needs. This class is featured rich and designed in way for making it possible to parse and format numbers in any local. It supports the Western, Arabic, and Indic digits.

How to format number in Java?

The DecimalFormat class also provide support for formatting in different kind of numbers such as integers (123), fixed-point numbers (123.4), scientific notation (1.23E4), percentages (12%), and currency amounts ($123).

How to use the DecimalFormat class?

First of all we have to import the DecimalFormat class in the application as shown below:

import java.text.DecimalFormat;

Then create the instance of the DecimalFormat class by specifying the format pattern as shown below:

DecimalFormat invoiceFormatter = new DecimalFormat("$0.00");

Here we are passing the pattern for formatting the number as a constructor argument to the DecimalFormat class. There are many different ways of writing the formatting pattern. You will find the descriptions and the examples of the pattern in this page.

Advertisement

Now you can format the data using the following code:

double invoiceValue= 40.429864;
System.out.println(invoiceFormatter.format(amount));

Here is the complete code of the example program:

import java.text.DecimalFormat;
import java.util.*;
/*
 *@Author: Deepak Kumar
 *Web: http://www.roseindia.net
*/
public class NumberFormatting
{
	public static void main(String[] args) 
	{
	  System.out.println("Number formatting Example!");
	  DecimalFormat invoiceFormatter = new DecimalFormat("$0.00");
	  double invoiceValue= 40.429864;
	  System.out.println(invoiceFormatter.format(invoiceValue));
	}
}

If you run the above code it will display the following output:

D:\examples\roseindia.net\corejava>java NumberFormatting
Number formatting Example!
$40.43

There may different ways of providing the formatting information to the DecimalFormat class. It follows a patters and here are the details of the pattern:

Symbol Location Localized? Meaning Example
0 Number Yes Digit - Used for digit in the pattern "$0.00"
# Number Yes Digit, zero shows as absent - It displays the digit if the data is present for that position and displays zero if data is not present "##,####,####"
. Number Yes It is the place holder for the decimal separator or monetary decimal separator "$0.00"
- Number Yes Minus sign "0.00;-0.00"
, Number Yes Grouping separator - Used for grouping the formatted data.  "#,##,###,####"
E Number Yes Separates mantissa and exponent in scientific notation. Need not be quoted in prefix or suffix. "0.00000E00"
; Subpattern boundary Yes Separates positive and negative subpatterns. It is used for the sub-patterns. "0.00;-0.00"
% Prefix or suffix Yes It is used to multiply the input by 100 and show as percentage  
\u2030 Prefix or suffix Yes It is used to multiply the input data by 1000 and show as per mille value  
¤(\u00A4) Prefix or suffix No This is used for Currency sign, replaced by currency symbol.  
' Prefix or suffix No Used to quote special characters in a prefix or suffix. for example. "'#'#" formats 123 to "#123"

Above pattern explains you how to use the patterns with the DecimalFormat class.

Other examples:

Check the more example of Java Beginners tutorials.