Skip to content

Formatting Numbers

Number Format Class

Java has an in built methods to assist in formatting numbers in the java.text package called NumberFormat.

import java.text.NumberFormat;

NumberFormat = currency = new NumberFormat()
// This will not work as the class is abstract (discussed more later)
// As the class is abstract it is not instantiated.
NumberFormat currency = NumberFormat.getCurrencyInstance();
// This would create an instance of the class and return it
// Instead of using new operaters we use these methods.
// These are called Factory Methods
String result = currency.format(1234567.891)
// This method would return a string representation of the number formatted as currency
// $1,234,567.89