How do I display negative number in parentheses?

The code snippet below show us how to display or format negative number in parentheses. We start by defining the number format, the pattern has two parts separated by a semicolon. In the snippet we use the #,##0.00;(#,##0.00) pattern. The pattern after the semicolon will be use to format negative number.

Next we create an instance of DecimalFormat by calling getInstance() method. We apply the the format pattern for the formatter object by calling the applyPattern() method of the DecimalFormat instance. To format the number we simply call the format() method and pass the number we are going to format for display or print out.

package org.kodejava.example.text;

import java.text.DecimalFormat;

public class NegativeNumberFormat {
    // Pattern for formatting negative number.
    public static final String PATTERN1 = "#,##0.00;(#,##0.00)";
    public static final String PATTERN2 = "$#,##0.00;-$#,##0.00";

    public static void main(String[] args) {
        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();
        df.applyPattern(PATTERN1);

        // Format using parentheses
        System.out.println("Positive: " + df.format(125));
        System.out.println("Negative: " + df.format(-125));

        // Format using currency symbol and minus sign
        df.applyPattern(PATTERN2);
        System.out.println("Positive: " + df.format(1000));
        System.out.println("Negative: " + df.format(-1000));
    }
}

The result of the code snippet above is:

Positive: 125.00
Negative: (125.00)
Positive: $1,000.00
Negative: -$1,000.00

If you need to parse negative numbers in parentheses to produce the represented number you can see the following example How do I parse negative number in parentheses?.

How do I format a number as currency string?

Creating a financial application requires you to format number as a currency. It should includes the correct thousands separator, decimal separator and the currency symbol. For this purpose you can use the NumberFormat.getCurrencyInstance() method and pass the correct Locale to get the currency format that you want.

package org.kodejava.example.text;

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

public class LocaleCurrencyFormat {
    public static void main(String[] args) {
        Double number = 1500D;

        //
        // Format currency for Canada locale in Canada locale, 
        // the decimal point symbol is a comma and currency
        // symbol is $.
        //
        NumberFormat format = 
                NumberFormat.getCurrencyInstance(Locale.CANADA);
        String currency = format.format(number);
        System.out.println("Currency in Canada : " + currency);

        //
        // Format currency for Germany locale in German locale,
        // the decimal point symbol is a dot and currency symbol
        // is €.
        //
        format = NumberFormat.getCurrencyInstance(Locale.GERMANY);
        currency = format.format(number);
        System.out.println("Currency in Germany: " + currency);
    }
}

Here is an output for the currency format using the Locale.CANADA and Locale.GERMANY.

Currency in Canada : $1,500.00
Currency in Germany: 1.500,00 €

How do I format a number with leading zeros?

This example give you an example using java.text.DecimalFormat class to add leading zeros to a number. For a another method you can read the see the earlier example on How do I add leading zeros to a number?.

package org.kodejava.example.lang;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class NumberFormatLeadingZerosExample {
    public static void main(String[] args) {
    NumberFormat formatter = new DecimalFormat("0000000");
    String number = formatter.format(2500);

    System.out.println("Number with lading zeros: " + number);
    }
}

The result of code snippet above:

Number with lading zeros: 0002500

How do I parse a number for a locale?

package org.kodejava.example.text;

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

public class LocaleNumberParse {
    public static void main(String[] args) {
        try {
            // In this example we are trying to parse a number string in a
            // defined format. Basically we want to covert the string for a
            // locale into a correct number value.
            Number number =
                NumberFormat.getNumberInstance(Locale.JAPAN).parse("25,000.75");

            // Just do some stuff with the number from the parse process
            if (number instanceof Long) {
                System.out.println("This number is instanceof Long and the " +
                    "value is: " + number.longValue());
            } else if (number instanceof Double) {
                System.out.println("This number is instanceof Double and the " +
                    "value is: " + number.doubleValue());
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}

How do I format a number for a locale?

package org.kodejava.example.text;

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

public class LocaleNumberFormat {
    public static void main(String[] args)  {
        // Format number for Italy locale. In Italy locale the decimal point
        // symbol is a comma.
        NumberFormat formatter = NumberFormat.getNumberInstance(Locale.ITALY);
        try {
            String number = formatter.format(195325.75);
            System.out.println("Number in Italy: " + number);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        // Format number for Japan locale. In Japan locale the decimal point
        // symbol is a dot.
        formatter = NumberFormat.getNumberInstance(Locale.JAPAN);
        try {
            String number = formatter.format(195325.75);
            System.out.println("Number in Japan: " + number);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
}

How do I format a number?

If you want to display some numbers that is formatted to a certain pattern, either in a Java Swing application or in a JSP file, you can utilize NumberFormat and DecimalFormat class to give you the format that you want. Here is a small example that will show you how to do it.

In the code snippet below we start by creating a double variable that contains some value. By default the toString() method of the double data type will print the money value using a scientific number format as it is greater than 10^7 (10,000,000.00). To be able to display the number without scientific number format we can use java.text.DecimalFormat which is a sub class of java.text.NumberFormat.

We create a formatter using DecimalFormat class with a pattern of #0.00. The # symbol means any number but leading zero will not be displayed. The 0 symbol will display the remaining digit and will display as zero if no digit is available.

package org.kodejava.example.text;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class DecimalFormatExample {
    public static void main(String[] args) {
        // We have some millions money here that we'll format its look.
        double money = 100550000.75;

        // Creates a formatter
        NumberFormat formatter = new DecimalFormat("#0.00");

        // Print the number using scientific number format and using our 
        // defined decimal format pattern above.
        System.out.println(money);
        System.out.println(formatter.format(money));
    }
}

Here is the different result of the code above.

1.0055000075E8
100550000.75