Number Formatting

To format numbers properly for output, the NumberFormat class of Java does a good job, and the DecimalFormat class can be even easier to use, once you get the trick to using it.. Here's an example of both:

Use these as you will, but beware that the way they are used may muddy your understanding of the two other more conventional ways of calling methods: via instances of classes, and directly from the class as with Math class methods. So there's no real need to commit this way of doing things to memory; rather just remember that this sample code is here under the "Number Formatting" link to be copied and pasted as needed.

/Users/adelaide/Public/Netbeans - All JSR Projects/Formatting/src/formattingpackage/FormatPlayingAround.java
public class FormatPlayingAround {

    public static void main(String[] args) {

        NumberFormat myFormat = NumberFormat.getCurrencyInstance();
        NumberFormat myFrenchFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);

        DecimalFormat formatter = new DecimalFormat("#00000000000.0 \u00a5");


        NumberFormat percent = NumberFormat.getPercentInstance();


The output of the above would be:

23 432,23 €
00000023423.0 ¥


So, at the risk of confusing you with the other more conventional ways of calling methods, here's how the above works.

NumberFormat is what's referred to as an abstract class which provides and interface for formatting. There is no need to understand in detail at this point how such classes work; just think of them as providing an idea more than a template to be used. Anyway, to make use of NumberFormat, you do indeed create an instance of it, but that instance does not use a constructor method; rather it directly accesses a method like getCurrencyInstance(). It is a void method which simply sets the currency format of your NumberFormat object. So if you then use the format method, it will format according to the last method which you called. So note above that you can then proceed to change the kind of currency, and the format method will then work differently.

The getCurrencyInstance method can either take no parameter, in which case you get the default (American) currency format, or you can use one of the set parameters from the Local class, which has unchangeable ("constant") attributes representing a variety of countries currency formats you can choose from.

And in terms of using NumberFormat for determining the maximum number of fractional and decimal digits, there are methods for this too.

And for currency symbol not in the list of "Locales", you can take advantage of a different class, and a different approach, which is overall arguably much user friendly, the DecimalFormat class. With it, you determine the number of integer digits, and the number of fractional digits with 0s on either side of the decimal, preceded by a # symbol. And you put the UNICODE value of the symbol you want behind the Java escape character backslash \.

In terms of the getPercentageInstance method of the NumberFormat class, just remember that your result will have the decimal point shifted two places to the right, as well as having the percent sign appended to the end of your output.