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();
        }
    }
}

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA. Support me by donating >> here <<.

Leave a Reply