标签:name nal apply 直接 welcome 通过 类实例化 googl 城市
背景
Locale类
获取区域和语言码
1 public class Locale_Demo { 2 public static void main(String[] args) { 3 Locale loc = Locale.CHINA; 4 System.out.println(loc); 5 } 6 }
指定区域和语言
1 public class Locale_Demo { 2 public static void main(String[] args) { 3 Locale loc = new Locale("zh","CN"); 4 System.out.println(loc); 5 } 6 }
获取当前默认区域和语言
1 public class Locale_Demo { 2 public static void main(String[] args) { 3 Locale loc = Locale.getDefault(); 4 System.out.println(loc); 5 } 6 }
ResourceBundle类
1 import java.util.Locale; 2 import java.util.ResourceBundle; 3 4 public class Locale_Demo { 5 public static void main(String[] args) { 6 Locale loc = Locale.US; 7 8 ResourceBundle resouceBundle = ResourceBundle.getBundle("a.Message",loc); 9 System.out.println(resouceBundle.getString("welcome.info")); 10 System.out.println(resouceBundle.getString("teacher.info")); 11 } 12 }
格式化文本
1 import java.text.MessageFormat; 2 import java.util.Locale; 3 import java.util.ResourceBundle; 4 5 public class Locale_Demo { 6 public static void main(String[] args) { 7 Locale loc = Locale.US; 8 9 ResourceBundle resourceBundle = ResourceBundle.getBundle("a.Message",loc); 10 String value = resourceBundle.getString("login.info"); 11 System.out.println(MessageFormat.format(value, "aaa","bbb")); 12 } 13 }
数字格式化
数字对象
1 import java.math.RoundingMode; 2 import java.text.DecimalFormat; 3 import java.text.NumberFormat; 4 5 public class Locale_Demo { 6 public static void main(String[] args) { 7 DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(); 8 numberFormat.applyPattern("####,####,####.000"); // 保留三位小数 9 numberFormat.setRoundingMode(RoundingMode.DOWN); // 不进位 10 numberFormat.setPositivePrefix("当年的收入流水:"); // 增加前置说明 11 numberFormat.setMinimumFractionDigits(5); // 保留五位小数 12 System.out.println(numberFormat.format(2826548.264785)); 13 } 14 }
百分比对象
1 import java.text.DecimalFormat; 2 import java.text.NumberFormat; 3 4 public class Locale_Demo { 5 public static void main(String[] args) { 6 NumberFormat numberFormat = (DecimalFormat) NumberFormat.getPercentInstance(); 7 System.out.println(numberFormat.format(0.9854623)); 8 // 自定义处理,强制向下转型 9 DecimalFormat decimalFormat = (DecimalFormat) numberFormat; 10 decimalFormat.setMinimumFractionDigits(5); 11 System.out.println(decimalFormat.format(0.9878546)); 12 } 13 }
货币对象
1 import java.text.NumberFormat; 2 import java.util.Locale; 3 4 public class Locale_Demo { 5 public static void main(String[] args) { 6 NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.CHINA); 7 System.out.println(numberFormat.format(789.46)); 8 } 9 }
参考
https://zhidao.baidu.com/question/414732825.html
标签:name nal apply 直接 welcome 通过 类实例化 googl 城市
原文地址:https://www.cnblogs.com/cxc1357/p/12452666.html