Custom Number Formatting in Java
In this example we will show you how to use the NumberFormat and DecimalFormat classes to format numbers using special patterns. We will also show you how to control the formatting of both leading and trailing zeros. In addition, we demonstrate how we can modify the formatting so that we can change the look and feel of the format to match our needs, as in the case of changing the default behavior of negative and show parenthesis instead of lead minus sign.
Custom Number Format Java Code
package com.omega.tutorial; import java.text.DecimalFormat; import java.text.NumberFormat; public class CustomNumberFormatExample { public static void main(String[] args) { final double number = -123456.789; final double percent = 0.285; final double tiny = 0.0001234; NumberFormat myFormat; DecimalFormat myDecimalFormat; String result; System.out.println("CustomNumberFormat Example"); System.out.println("--------------------------"); // Fixed pattern, with blank substituted if no digit present myFormat = new DecimalFormat("####"); result = myFormat.format(number); System.out.println("Pattern #1: " + result); // Fixed pattern, with decimals, 0 substituted if no digit present myFormat = new DecimalFormat("####.##"); result = myFormat.format(number); System.out.println("Pattern #2: " + result); // Display in exponential notation myFormat = new DecimalFormat("###E00"); result = myFormat.format(number); System.out.println("Pattern #3: " + result); myFormat = new DecimalFormat("###E00"); result = myFormat.format(tiny); System.out.println("Pattern #4: " + result); myFormat = new DecimalFormat("00.###E0"); result = myFormat.format(tiny); System.out.println("Pattern #5: " + result); // Comma separated format myFormat = new DecimalFormat("###,###.##"); result = myFormat.format(number); System.out.println("Pattern #6: " + result); // Show negative with parenthesis, comma separated format myDecimalFormat = new DecimalFormat("##,###,###.##"); myDecimalFormat.setNegativePrefix("("); myDecimalFormat.setNegativeSuffix(")"); result = myDecimalFormat.format(number); System.out.println("Pattern #7: " + result); // Comma separated format, with dollar currency (US) myFormat = new DecimalFormat("$ ###,###.##"); result = myFormat.format(number); System.out.println("Pattern #8: " + result); // Fixed pattern, with 0 substituted if no digit present myFormat = new DecimalFormat("0000"); result = myFormat.format(number); System.out.println("Pattern #9: " + result); myFormat = new DecimalFormat("00,000,000.0000"); result = myFormat.format(number); System.out.println("Pattern #10: " + result); myFormat = new DecimalFormat("########.000000"); result = myFormat.format(number); System.out.println("Pattern #11: " + result); myFormat = new DecimalFormat("####.0000"); result = myFormat.format(number); System.out.println("Pattern #12: " + result); myFormat = new DecimalFormat("###,###.0000"); result = myFormat.format(number); System.out.println("Pattern #13: " + result); // Show as percentage, multiples by 100 and adds % sign myFormat = new DecimalFormat("###%"); result = myFormat.format(percent); System.out.println("Pattern #14: " + result); } }
Output:
Core Java Related Tutorials
- Base64 Encoding and Decoding Examples in Java 8
In this tutorial we will discuss how to Encode and Decode using Base64 using Java 8, which now finally has native Base64 support. - Base64 Encoding and Decoding Examples in Java using Google Guava
This tutorial will introduce how to Encode and Decode using Base64 using Google’s Guava Project Open Source library. - Base64 Encoding and Decoding Examples in Java using Apache Commons
This tutorial will introduce Base64 encoding and decoding examples using the Apache Commons Codec library. - Custom Number Formatting in Java
In this example we will show you how to use the NumberFormat and DecimalFormat classes to format numbers using special patterns. - Custom Date Formatting in Java
In this example we will show you how to use the SimpleDateFormat class to format Date objects using special patterns to better fit the needs of the application.
Please Share Us on Social Media






Leave a Reply