在国际化程序中,在不同国家中,千分位,小数点和分隔符是不相同的。这样在处理数字和货币的时候需要特别注意,在Java API中,可以把国家对应的Locale信息传给NumberFormat和DecimalFormatSymbols,然后进行相应的处理。
另外,在处理微软Excel的时候,不同国家的CSV分隔符也是不一样的,需要同样的方法进行处理。
下面是一个例子,可以看到不同国家的千分位和小数点,以及数字显示
代码
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class NumberFormatForLocaleTest {
public static void main(String[] args) throws ParseException, IOException {
System.out.println(System.getProperty("java.version"));
List<Locale> allLocale = new ArrayList<Locale>();
allLocale.add(new Locale("bs", "ID"));
allLocale.add(new Locale("bs", "BS"));
allLocale.add(new Locale("cs", "CZ"));
allLocale.add(new Locale("da", "DK"));
allLocale.add(new Locale("de", "DE"));
allLocale.add(new Locale("en", "GB"));
allLocale.add(new Locale("en", "US"));
allLocale.add(new Locale("es", "ES"));
allLocale.add(new Locale("fr", "FR"));
allLocale.add(new Locale("fr", "CA"));
allLocale.add(new Locale("hr", "HR"));
allLocale.add(new Locale("it", "IT"));
allLocale.add(new Locale("hu", "HU"));
allLocale.add(new Locale("nl", "NL"));
allLocale.add(new Locale("no", "NO"));
allLocale.add(new Locale("nb", "NO"));
allLocale.add(new Locale("pl", "PL"));
allLocale.add(new Locale("pt", "PT"));
allLocale.add(new Locale("pt", "BR"));
allLocale.add(new Locale("ro", "RO"));
allLocale.add(new Locale("de", "CH"));
allLocale.add(new Locale("sk", "SK"));
allLocale.add(new Locale("sl", "SI"));
allLocale.add(new Locale("sr", "RS"));
allLocale.add(new Locale("fi", "FI"));
allLocale.add(new Locale("sv", "SE"));
allLocale.add(new Locale("sv", "SW"));
allLocale.add(new Locale("tl", "PH"));
allLocale.add(new Locale("vi", "VN"));
allLocale.add(new Locale("tr", "TR"));
allLocale.add(new Locale("el", "GR"));
allLocale.add(new Locale("en", "DEBUG"));
allLocale.add(new Locale("ru", "RU"));
allLocale.add(new Locale("bg", "BG"));
allLocale.add(new Locale("uk", "UA"));
allLocale.add(new Locale("iw", "IL"));
allLocale.add(new Locale("hi", "IN"));
allLocale.add(new Locale("th", "TH"));
allLocale.add(new Locale("ko", "KR"));
allLocale.add(new Locale("ja", "JP"));
allLocale.add(new Locale("zh", "CN"));
allLocale.add(new Locale("zh", "TW"));
// Locale[] locales = Locale.getAvailableLocales();
// allLocale = Arrays.asList(locales);
System.out.println("| Locale| Thousand | Decimal| Example");
for (Locale locale : allLocale) {
if (locale.getCountry().length() == 0) {
continue;
}
System.out.println(locale.getCountry());
System.out.println(locale.getDisplayCountry());
NumberFormat nf = NumberFormat.getInstance(locale);
DecimalFormatSymbols dfs = ((DecimalFormat) nf).getDecimalFormatSymbols();
String group = "single quote(')";
if (dfs.getGroupingSeparator() == '\u00a0') {
group = "space( )";
} else if (dfs.getGroupingSeparator() == ',') {
group = "comma(,)";
} else if (dfs.getGroupingSeparator() == '.') {
group = "dot(.)";
}
String decimal = "comma(,)";
if (dfs.getDecimalSeparator() == '.') {
decimal = "dot(.)";
}
System.out.println("| " + locale.toString() + " | " + group + " | "
+ decimal + " | " + nf.format(4000.04) + " |");
}
}
}
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class NumberFormatForLocaleTest {
public static void main(String[] args) throws ParseException, IOException {
System.out.println(System.getProperty("java.version"));
List<Locale> allLocale = new ArrayList<Locale>();
allLocale.add(new Locale("bs", "ID"));
allLocale.add(new Locale("bs", "BS"));
allLocale.add(new Locale("cs", "CZ"));
allLocale.add(new Locale("da", "DK"));
allLocale.add(new Locale("de", "DE"));
allLocale.add(new Locale("en", "GB"));
allLocale.add(new Locale("en", "US"));
allLocale.add(new Locale("es", "ES"));
allLocale.add(new Locale("fr", "FR"));
allLocale.add(new Locale("fr", "CA"));
allLocale.add(new Locale("hr", "HR"));
allLocale.add(new Locale("it", "IT"));
allLocale.add(new Locale("hu", "HU"));
allLocale.add(new Locale("nl", "NL"));
allLocale.add(new Locale("no", "NO"));
allLocale.add(new Locale("nb", "NO"));
allLocale.add(new Locale("pl", "PL"));
allLocale.add(new Locale("pt", "PT"));
allLocale.add(new Locale("pt", "BR"));
allLocale.add(new Locale("ro", "RO"));
allLocale.add(new Locale("de", "CH"));
allLocale.add(new Locale("sk", "SK"));
allLocale.add(new Locale("sl", "SI"));
allLocale.add(new Locale("sr", "RS"));
allLocale.add(new Locale("fi", "FI"));
allLocale.add(new Locale("sv", "SE"));
allLocale.add(new Locale("sv", "SW"));
allLocale.add(new Locale("tl", "PH"));
allLocale.add(new Locale("vi", "VN"));
allLocale.add(new Locale("tr", "TR"));
allLocale.add(new Locale("el", "GR"));
allLocale.add(new Locale("en", "DEBUG"));
allLocale.add(new Locale("ru", "RU"));
allLocale.add(new Locale("bg", "BG"));
allLocale.add(new Locale("uk", "UA"));
allLocale.add(new Locale("iw", "IL"));
allLocale.add(new Locale("hi", "IN"));
allLocale.add(new Locale("th", "TH"));
allLocale.add(new Locale("ko", "KR"));
allLocale.add(new Locale("ja", "JP"));
allLocale.add(new Locale("zh", "CN"));
allLocale.add(new Locale("zh", "TW"));
// Locale[] locales = Locale.getAvailableLocales();
// allLocale = Arrays.asList(locales);
System.out.println("| Locale| Thousand | Decimal| Example");
for (Locale locale : allLocale) {
if (locale.getCountry().length() == 0) {
continue;
}
System.out.println(locale.getCountry());
System.out.println(locale.getDisplayCountry());
NumberFormat nf = NumberFormat.getInstance(locale);
DecimalFormatSymbols dfs = ((DecimalFormat) nf).getDecimalFormatSymbols();
String group = "single quote(')";
if (dfs.getGroupingSeparator() == '\u00a0') {
group = "space( )";
} else if (dfs.getGroupingSeparator() == ',') {
group = "comma(,)";
} else if (dfs.getGroupingSeparator() == '.') {
group = "dot(.)";
}
String decimal = "comma(,)";
if (dfs.getDecimalSeparator() == '.') {
decimal = "dot(.)";
}
System.out.println("| " + locale.toString() + " | " + group + " | "
+ decimal + " | " + nf.format(4000.04) + " |");
}
}
}