Format Number Java

If you want to format number java in predefined format then use NumberFormat class and invoke its method. You could format percentage, numbers and currencies based on its locale. Please have below sample java class which shows formatting techniques:

  • FormatNumberJava.java:
package com.barcap.fo.util;

import java.text.NumberFormat;
import java.util.Locale;

public class FormatNumberJava {

	public static void main(String[] args) {		
		formatNumber(new Locale("en_US"));
		formatNumber(new Locale("de_DE"));
	}
	
	private static void formatNumber(Locale locale) {

	    Integer quantity = new Integer(987456321);
	    Double amount = new Double(987456321.369);
	    
	    NumberFormat numberFormatter = NumberFormat.getNumberInstance(locale);
	    String quantityOut = numberFormatter.format(quantity);
	    String amountOut = numberFormatter.format(amount);
	    System.out.println("Fomated quantity value: "+quantityOut + " Locale:" + locale.toString());
	    System.out.println("Fomated amount value: "+amountOut + " Locale:" + locale.toString());
	}

}
  • Output:

Format Number Java

  • That’s it for format number java for more details please visit oracle tutorial here

Leave a Reply

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