zoukankan      html  css  js  c++  java
  • JavaWeb-国际化之NumberFormat

    NumberFormat类

    * NumberFormat可以将一个数值格式化为符合某个国家地区习惯的数值字符串,也可以将符合某个国家地区习惯的数值字符串解析为对应的数值

    * NumberFormat类的方法:

    format方法:将一个数值格式化为符合某个国家地区习惯的数值字符串

    —parse方法:符合某个国家地区习惯的数值字符串解析为对应的数值

    NumberFormat:格式化数字到数字字符串,或货币字符串的工具类

    1.通过工厂方法获取NumberFormat对象

    NumberFormat.getNumberInstance(locale);//仅格式化为数字的字符串

    NumberFormat.getCurrencyInstance(locale);//格式为货币的字符串

    2.通过format方法进行格式化

    3.通过parse方法把一个字符串解析为一个number类型

       @Test
        public void testNumberFormat() throws ParseException {
            double d = 123456789.123d;
            Locale locale = Locale.CHINA;
            NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
            String str = numberFormat.format(d);
            System.out.println(str);
    
           NumberFormat numberFormat2 = NumberFormat.getCurrencyInstance(locale);
            str = numberFormat.format(d);
            System.out.println(str);
            str = "123,456,789.123";
            d = (double) numberFormat.parse(str);
            System.out.println(d);
    
            str = "¥123,456,789.123";
            d = (double) numberFormat2.parse(str);
            System.out.println(d);
        }
    

      

  • 相关阅读:
    176. Second Highest Salary
    175. Combine Two Tables
    172. Factorial Trailing Zeroes
    171. Excel Sheet Column Number
    169. Majority Element
    168. Excel Sheet Column Title
    167. Two Sum II
    160. Intersection of Two Linked Lists
    个人博客记录
    <meta>标签
  • 原文地址:https://www.cnblogs.com/yangHS/p/11232170.html
Copyright © 2011-2022 走看看