Home » Core Java » text » Format number with custom NumberFormat

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Format number with custom NumberFormat

In this example we shall show you how to format a number with a custom NumberFormat. NumberFormat provides the interface for formatting and parsing numbers. It also provides methods for determining which locales have number formats, and what their names are. To format a number with custom NumberFormat one should perform the following steps:

  • Create a new DecimalFormat, using a String pattern. The pattern can consist of zeros, which will set a digit or 0 if no digit present, a #, which will set a digit or nothing if no digit present. We can use the decimal point in the pattern, or the comma, to group numbers. We can also use ‘, in order to quote literal symbols or text.
  • Invoke the format(double number) API method of the DecimalFormat, using a double number as parameter in order to create the formatted String value,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class FormatNumberWithCustomNumberFormat {
	
	public static void main(String[] args) {
		
		NumberFormat formatter;
		String number;
		
		// 0 --> a digit or 0 if no digit present
		formatter = new DecimalFormat("00000");
		number = formatter.format(-1234.567);
		System.out.println("Number 1: " + number);
		
		formatter = new DecimalFormat("0000.000");
		number = formatter.format(-1234.567);
		System.out.println("Number 2: " + number);

		// # --> a digit or nothing if no digit present
		formatter = new DecimalFormat("##");
		number = formatter.format(-1234.567);
		System.out.println("Number 3: " + number);
		
		formatter = new DecimalFormat("##00");
		number = formatter.format(0);
		System.out.println("Number 4: " + number);

		// . --> the decimal point
		formatter = new DecimalFormat(".00");
		number = formatter.format(-0.123);
		System.out.println("Number 5: " + number);
		
		formatter = new DecimalFormat("0.00");
		number = formatter.format(-0.123);
		System.out.println("Number 6: " + number);
		
		formatter = new DecimalFormat("#.#");
		number = formatter.format(-1234.567);
		System.out.println("Number 7: " + number);
		
		formatter = new DecimalFormat("#.######");
		number = formatter.format(-1234.567);
		System.out.println("Number 8: " + number);
		
		formatter = new DecimalFormat(".######");
		number = formatter.format(-1234.567);
		System.out.println("Number 9: " + number);
		
		formatter = new DecimalFormat("#.000000");
		number = formatter.format(-1234.567);
		System.out.println("Number 10: " + number);

		// , --> used to group numbers
		formatter = new DecimalFormat("#,###,###");
		number = formatter.format(-1234.567);
		System.out.println("Number 11: " + number);
		
		number = formatter.format(-1234567.890);
		System.out.println("Number 12: " + number);

		// '--> used to quote literal symbols
		formatter = new DecimalFormat("'#'#");
		number = formatter.format(-1234.567);
		System.out.println("Number 13: " + number);
		
		formatter = new DecimalFormat("'text'#");
		number = formatter.format(-1234.567);
		System.out.println("Number 14: " + number);
		
	}

}

Output:

Number 1: -01235
Number 2: -1234.567
Number 3: -1235
Number 4: 00
Number 5: -.12
Number 6: -0.12
Number 7: -1234.6
Number 8: -1234.567
Number 9: -1234.567
Number 10: -1234.567000
Number 11: -1,235
Number 12: -1,234,568
Number 13: -#1235
Number 14: -text1235

 
This was an example of how to format a number with custom NumberFormat in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of