Java Number Format

Last modified on August 1st, 2014 by Joe.

When you work in an internationalized application java number formats are going to be a head ache. Always use appropriate tools for its respective task. Though you can use a sledge hammer to open a can it is not meant for that. And there is a high possibility that the can might break.

Formatting

If you are a superficial programmer you may tend to do all the formatting using StringBuilder itself. When it comes to internationalization and localization then it is good to use NumberFomat for all your formatting needs. Otherwise, your numbers will not be unique in all locales.

To use number format, first you have to get a locale instance. Then you can set many attributes of the formatting. Like, you have the option to show comma, restrict the number of decimal places, and set minimum and maximum integer length. If you want to display ‘%’ with respect to locale then you have to use NumberFormat. Just don’t append ‘%’ as a string to your result. Do you want to show paranthesis like (3745) in place of “-” to denote a negative numer, then use NumberFormat. Like these, there are numerous uses. Check javadoc of NumberFormat for more methods.

Convert (Parse) a String to Number

You can use parse to read from a String and convert it to a number. You should get the returned result in Number class as it is the super class of many wrapper classes. Always remember a rule, “program to interface”.

If you are looking for using custom for-matter like ####.## and parse numbers accordingly you need to know DecimalFormat. I will write about it in my next post.

import java.text.NumberFormat;
import java.text.ParseException;

public class NumberFormatExample {

	public static void main(String args[]) {
		// get a object for your locale
		NumberFormat numberFormat = NumberFormat.getInstance();

		// setting number of decimal places
		numberFormat.setMinimumFractionDigits(2);
		numberFormat.setMaximumFractionDigits(2);

		// you can also define the length of integer
		// that is the count of digits before the decimal point
		numberFormat.setMinimumIntegerDigits(1);
		numberFormat.setMaximumIntegerDigits(10);

		// if you want the number format to have commas
		// to separate the decimals the set as true
		numberFormat.setGroupingUsed(true);

		// convert from integer to String
		String formattedNr = numberFormat.format(12345678L);
		// note that the output will have 00 in decimal place
		System.out.println("12345678L number formatted to " + formattedNr);

		// convert from decimal to String
		formattedNr = numberFormat.format(12345.671D);
		System.out.println("12345.671D number formatted to " + formattedNr);

		// format a String to number
		Number n1 = null;
		Number n2 = null;

		try {
			n1 = numberFormat.parse("1,234");
			n2 = numberFormat.parse("1.234");
		} catch (ParseException e) {
			System.out.println("I couldn't parse your string!");
		}
		System.out.println(n1);
		System.out.println(n2);

		// show percentage
		numberFormat = NumberFormat.getPercentInstance();
		System.out.println(numberFormat.format(0.98));
	}
}

Output

12345678L number formatted to 12,345,678.00
12345.671D number formatted to 12,345.67
1234
1.234
98%

Comments on "Java Number Format"

  1. […] Decimal NumbersOctober 19th, 2009DecimalFormat class which is extended from NumberFormat allows you to format a number (both decimal and integer) into a beautified String. It can be used […]

  2. Anuja says:

    Thankx a lot

  3. Chintan says:

    Joe, Good Post.
    thanks

  4. ramya says:

    we need numberformat class methods by using locale object..can u please post it…..

  5. scs says:

    superb tutorial keep posting

  6. Chandra Mouli says:

    thanks nice explaniation with gud example.

  7. Anonymous says:

    Greattt…
    thanks…..

  8. Mohd Ikram says:

    Greattt…
    Thanksssss..

  9. J5SE says:

    I think you need add a example to parse a String value to Double , with a Locale format.
    Many application to use a String as currency value, is wrong, but can be used also.

  10. Sumeet Saini says:

    Very nice explanation Sir g

Comments are closed for "Java Number Format".