集册 Java实例教程 各种语言环境的数字格式

各种语言环境的数字格式

欢马劈雪     最近更新时间:2020-01-02 10:19:05

583
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
各种语言环境的数字格式
/*来自 n o w    j a v a  . c o m*/

import java.util.*;

import java.text.*;


public class NumberFormatDemo {


    static public void displayNumber(Locale currentLocale) {

        Integer quantity = new Integer(123456);

        Double amount = new Double(345987.246);

        NumberFormat numberFormatter;

        String quantityOut;

        String amountOut;


        numberFormatter = NumberFormat.getNumberInstance(currentLocale);

        quantityOut = numberFormatter.format(quantity);

        amountOut = numberFormatter.format(amount);

        System.out.println(quantityOut + "   " + currentLocale.toString());/** n o w    j a v a  . c o m 提 供 **/

        System.out.println(amountOut + "   " + currentLocale.toString());

    }


    static public void displayCurrency(Locale currentLocale) {

        Double currencyAmount = new Double(9876543.21);

        Currency currentCurrency = Currency.getInstance(currentLocale);

        NumberFormat currencyFormatter = NumberFormat

                .getCurrencyInstance(currentLocale);

        System.out.println(currentLocale.getDisplayName() + ", "

                + currentCurrency.getDisplayName() + ": "

                + currencyFormatter.format(currencyAmount));

    }


    static public void displayPercent(Locale currentLocale) {

        Double percent = new Double(0.75);

        NumberFormat percentFormatter;

        String percentOut;


        percentFormatter = NumberFormat.getPercentInstance(currentLocale);

        percentOut = percentFormatter.format(percent);

        System.out.println(percentOut + "   " + currentLocale.toString());

    }


    static public void main(String[] args) {

        ArrayList<Locale> locales = new ArrayList<>();

        locales.add(0,

                new Locale.Builder().setLanguage("fr").setRegion("FR")

                        .build());

        locales.add(1,

                new Locale.Builder().setLanguage("de").setRegion("DE")

                        .build());

        locales.add(2,

                new Locale.Builder().setLanguage("en").setRegion("US")

                        .build());


        for (int i = 0; i < locales.size(); i++) {

            displayNumber(locales.get(i));

        }

        for (int i = 0; i < locales.size(); i++) {

            displayCurrency(locales.get(i));

        }

        for (int i = 0; i < locales.size(); i++) {

            displayPercent(locales.get(i));

        }

    }

}


展开阅读全文