Poechant 解读 Java API —— 借助货币格式化,初识 Locale 和 NumberFormat

Locale 属于 java.util.Locale 包

NumberFormat 属于 java.text.NumberFormat 包

	NumberFormat java.text.NumberFormat.getCurrencyInstance();
	NumberFormat java.text.NumberFormat.getCurrencyInstance(java.util.Locale);
	String java.util.NumberFormat.format(long);
	String java.util.NumberFormat.format(int);
	String java.util.NumberFormat.format(double);
	String java.util.NumberFormat.format(float);

入门的使用是比较简单的,以下面代码为例

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

public class Test {
	public static void main(String[] args) {
		LocaleTest.print();
	}
}

class LocaleTest {
	static public void print() {
		String countries[] = {
				"China",
				"Canada",
				"Korea",
				"Taiwan",
				"France",
				"Italy",
				"Japan",
				"Germany",
				"US",
				"UK"
		};
		Locale locale[] = {
				Locale.CHINA,
				Locale.CANADA,
				Locale.KOREA,
				Locale.TAIWAN,
				Locale.FRANCE,
				Locale.ITALY,
				Locale.JAPAN,
				Locale.GERMANY,
				Locale.US,
				Locale.UK
		};
		int len = countries.length;
		for (int i = 0; i < len; ++i) {
			System.out.println(countries[i] + ": " + NumberFormat.getCurrencyInstance(locale[i]).format(120000));
		}
	}
}

原文链接: http://blog.csdn.net/poechant/article/details/6919230

你可能感兴趣的:(Poechant 解读 Java API —— 借助货币格式化,初识 Locale 和 NumberFormat)