企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # NumberFormat类(数字格式化) NumberFormat类可以将一个数值格式化为符合某个国家地区习惯的数值字符串,也可以将符合某个国家地区习惯的数值字符串解析为对应的数值。 NumberFormat类的方法: * format方法:将一个数值格式化为符合某个国家地区习惯的数值字符串。 * parse方法:将符合某个国家地区习惯的数值字符串解析为对应的数值。 实例化NumberFormat类时,可以使用locale对象作为参数,也可以不使用,下面列出的是使用参数的。 * getNumberInstance(Locale locale):以参数locale对象所标识的本地信息来获得具有多种用途的NumberFormat实例对象。 * getIntegerInstance(Locale locale):以参数locale对象所标识的本地信息来获得处理整数的NumberFormat实例对象。 * getCurrencyInstance(Locale locale):以参数locale对象所标识的本地信息来获得处理货币的NumberFormat实例对象。 * getPercentInstance(Locale locale):以参数locale对象所标识的本地信息来获得处理百分比数值的NumberFormat实例对象 # 例子 ~~~ public static void main(String[] args) throws ParseException { int price = 18; // $18 ¥18 // NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA); // ¥18.00 // NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); // $18.00 // NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.JAPAN); // ¥18 NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.FRANCE); // 18,00 € System.out.println(nf.format(price)); String s = "18,00 €"; nf = NumberFormat.getCurrencyInstance(Locale.FRANCE); System.out.println(nf.parse(s).doubleValue()); // 18.0 double d = 0.1; nf = NumberFormat.getPercentInstance(); System.out.println(nf.format(d)); // 10% } ~~~ # 占位符的三种书写方式 占位符有三种方式书写方式: * `{argumentIndex}`:0-9之间的数字,表示要格式化对象数据在参数数组中的索引号。 * `{argumentIndex,formatType}`:参数的格式化类型。 * `{argumentIndex,formatType,FormatStyle}`:格式化的样式,它的值必须是与格式化类型相匹配的合法模式、或表示合法模式的字符串。 ~~~ public static void main(String[] args) { String pattern = "At {0, time, short} on {0, date}, a destroyed {1} houses and caused {2, number, currency} of damage."; MessageFormat format = new MessageFormat(pattern, Locale.US); Object[] params = {new Date(), 99, 100000000}; String message = format.format(params); System.out.println(message); } ~~~ 运行结果为:At 9:06 AM on Aug 18, 2016, a destroyed 99 houses and caused $100,000,000.00 of damage. 以上就是JavaWeb开发中国际化的总结内容