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:

number_format_output

Core Java Related Tutorials

Please Share Us on Social Media

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *