NumberFormat DecimalFormat

http://blog.csdn.net/evangel_z/article/details/7624503

http://blog.163.com/wangzhengquan85@126/blog/static/36082995201182111847325/

http://niuxiaoxia870563296.iteye.com/blog/1749966

To format a number for the current Locale, use one of the factory class methods:
  myString = NumberFormat.getInstance().format(myNumber);
If you are formatting multiple numbers, it is more efficient to get the format and use it multiple times so that the system doesn't have to fetch the information about the local language and country conventions multiple times.
  NumberFormat nf = NumberFormat.getInstance();
  for (int i = 0; i < myNumber.length; ++i) {
    output.println(nf.format(myNumber[i]) + "; ");
  }

To format a number for a different Locale, specify it in the call to getInstance.
  NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
You can also use a NumberFormat to parse numbers:
  myNumber = nf.parse(myString);

Use getInstance or getNumberInstance to get the normal number format. Use getIntegerInstance to get an integer number format. Use getCurrencyInstance to get the currency number format. And use getPercentInstance to get a format for displaying percentages. With this format, a fraction like 0.53 is displayed as 53%.

DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字。该类设计有各种功能,使其能够分析和格式化任意语言环境中的数,包括对西方语言、阿拉伯语和印度语数字的支持。它还支持不同类型的数,包括整数 (123)、定点数 (123.4)、科学记数法表示的数 (1.23E4)、百分数 (12%) 和金额 ($123)。所有这些内容都可以本地化。
DecimalFormat 包含一个模式 和一组符号。可直接使用 applyPattern() 或间接使用 API 方法来设置模式

符号 位置 本地化? 含义
0 数字 阿拉伯数字
# 数字字 阿拉伯数字,如果不存在则显示为空
. 数字 小数分隔符或货币小数分隔符
- 数字 减号
, 数字 分组分隔符
E 数字 分隔科学计数法中的尾数和指数。在前缀或后缀中无需加引号。
; 子模式边界 分隔正数和负数子模式
% 前缀或后缀 乘以 100 并显示为百分数
/u2030 前缀或后缀 乘以 1000 并显示为千分数
¤(/u00A4) 前缀或后缀 货币记号,由货币符号替换。如果两个同时出现,则用国际货币符号替换。如果出现在某个模式中,则使用货币小数分隔符,而不使用小数分隔符。
' 前缀或后缀 用于在前缀或或后缀中为特殊字符加引号,例如 "'#'#" 将 123 格式化为 "#123"。要创建单引号本身,请连续使用两个单引号:"# o''clock"
 1 package com.java.numberformat;
 2 
 3 import java.text.DecimalFormat;
 4 import java.util.Locale;
 5 
 6 public class DecimalFormatTest {
 7     public static void main(String[] args) {
 8         test1();
 9         test2();
10         test3();
11     }
12     
13     public static void test1(){
14         System.out.println("---------- new DecimalFormat(String format) -----------");
15         DecimalFormat df1 = new DecimalFormat("0.0");   
16         DecimalFormat df2 = new DecimalFormat("#.#");   
17         DecimalFormat df3 = new DecimalFormat("000.000");   
18         DecimalFormat df4 = new DecimalFormat("###.###");   
19         System.out.println("12.34 ---->  0.0 ----> " + df1.format(12.34));   
20         System.out.println("12.34 ----> #.# ----> " + df2.format(12.34));   
21         System.out.println("12.34 ----> 000.000 ----> " + df3.format(12.34));   
22         System.out.println("12.34 ----> ###.### ----> " + df4.format(12.34));   
23     }
24     
25     public static void test2(){
26         System.out.println("---------- DecimalFormat applyPattern -----------");
27         DecimalFormat df = new DecimalFormat();   
28         df.applyPattern("0.0");
29         System.out.println("12.34 ----> 0.0 ----> " + df.format(12.34)); 
30         df.applyPattern("#.#");
31         System.out.println("12.34 ----> #.# ----> " + df.format(12.34));    
32         df.applyPattern("000.000");
33         System.out.println("12.34 ----> 000.000 ----> " + df.format(12.34));   
34         df.applyPattern("###.###");
35         System.out.println("12.34 ----> ###.### ----> " + df.format(12.34));   
36     }
37     
38     public static void test3() {
39         DecimalFormat format = new DecimalFormat("###,####.000");   
40         System.out.println("111111123456.1227222 ---> ###,####.000 ---->" + format.format(111111123456.1227222));   
41           
42         Locale.setDefault(Locale.US);   
43         DecimalFormat usFormat = new DecimalFormat("###,###.000");   
44         System.out.println("111111123456.1227222 ---> ###,###.000 ---->" + usFormat.format(111111123456.1227222));   
45           
46         DecimalFormat addPattenFormat = new DecimalFormat();   
47         addPattenFormat.applyPattern("##,###.000");   
48         System.out.println("111111123456.1227 ---> ##,###.000 ---->" + addPattenFormat.format(111111123456.1227));   
49           
50         DecimalFormat zhiFormat = new DecimalFormat();   
51         zhiFormat.applyPattern("0.000E0000");   
52         System.out.println("10000 ---> 0.000E0000 ---->" + zhiFormat.format(10000));   
53         System.out.println("12345678.3452 ---> 0.000E0000 ---->" + zhiFormat.format(12345678.345));   
54           
55         DecimalFormat percentFormat = new DecimalFormat();   
56         percentFormat.applyPattern("#0.000%");   
57         System.out.println("0.3052222 ---> #0.000% ---->" + percentFormat.format(0.3052222));  
58     }
59 }

----- Console output -----

---------- new DecimalFormat(String format) -----------
12.34 ----> 0.0 ----> 12.3
12.34 ----> #.# ----> 12.3
12.34 ----> 000.000 ----> 012.340
12.34 ----> ###.### ----> 12.34
---------- DecimalFormat applyPattern -----------
12.34 ----> 0.0 ----> 12.3
12.34 ----> #.# ----> 12.3
12.34 ----> 000.000 ----> 012.340
12.34 ----> ###.### ----> 12.34
111111123456.1227222 ---> ###,####.000 ---->1111,1112,3456.123
111111123456.1227222 ---> ###,###.000 ---->111,111,123,456.123
111111123456.1227 ---> ##,###.000 ---->111,111,123,456.123
10000 ---> 0.000E0000 ---->1.000E0004
12345678.3452 ---> 0.000E0000 ---->1.235E0007
0.3052222 ---> #0.000% ---->30.522%


DecimalFormat是NumberFormat的一个子类,其实例被指定为特定的地区。因此,你可以使用NumberFormat.getInstance 指定一个地区,然后将结构强制转换为一个DecimalFormat对象。文档中提到这个技术可以在大多情况下适用,但是你需要用try/catch 块包围强制转换以防转换不能正常工作 (大概在非常不明显得情况下使用一个奇异的地区)。
下面是一个这样的例子:

 1 import java.text.DecimalFormat;
 2 import java.text.NumberFormat;
 3 import java.util.Locale;
 4 public class DecimalFormat6 {
 5 public static void main(String args[]) {
 6     DecimalFormat df = null;
 7     // 得到一个NumberFormat 对象并
 8     // 强制转换为一个 DecimalFormat 对象
 9     try {
10         df = (DecimalFormat)NumberFormat.getInstance(Locale.GERMAN);
11     } catch (ClassCastException e) {
12     System.err.println(e);
13     }
14     // 设置格式模式
15     df.applyPattern("####.00000");
16     // format a number
17     System.out.println(df.format(1234.56));
18     }
19 }    

如果你不关心国际化,可以直接使用DecimalFormat 。

  1 class Test {
  2     public static void main(String[] args) {
  3         new Test().test();
  4     }
  5 
  6     public void test() {
  7         double d = 3.1415926;
  8         double d1 = 100.2;
  9         long l = 123456789;
 10         String s = "987,3.1415926";
 11         // NumberFormat nf = NumberFormat.getInstance();
 12         // nf.setMaximumFractionDigits(2);
 13         // nf.setMinimumFractionDigits(2);
 14         // nf.setMinimumIntegerDigits(2);
 15         // nf.setMaximumIntegerDigits(2);
 16         // System.out.println(nf.format(d));//03.14
 17         // System.out.println(nf.format(d1));//00.12
 18         DecimalFormat df = new DecimalFormat();
 19         // 0占位符:显示pattern规定的位数,没有用0补齐
 20         // df.applyPattern("0.00");//3.14 100.20
 21         // df.applyPattern("000.0000");//003.1416 100.2000
 22         // df.applyPattern("000");//003 100
 23         // #占位符:显示pattern规定的位数,没有无需补充
 24         // df.applyPattern("##.###");//3.142 100.2
 25         // df.applyPattern("#");//3 100
 26         // 以百分数形式显示
 27         // df.applyPattern("#.00%");//314.16% 10020.00
 28         // df.applyPattern("#.#%");//314.2% 10020%
 29         // 以千分数显示,并取三位小数,不够用0补齐
 30         // df.applyPattern("#.000u2030");//3141.593‰ 100200.000‰
 31         // 添加分组分隔符 、负号
 32         // df.applyPattern("-,###.##");//123,456,789
 33         // 嵌入文本
 34         // df.applyPattern("看看-,###.##ok");//看看-123,456,789ok 看看-100.2ok
 35         // 解析文本成Number
 36         Number n = null;
 37         try {
 38             n = df.parse(s);// 9873.1415926
 39         } catch (ParseException e) {
 40             e.printStackTrace();
 41         }
 42         System.out.println(String.valueOf(n));
 43         System.out.println(df.format(d1));
 44     }
 45 
 46     /**
 47      * NumberFormat:public abstract class NumberFormat,是所有数值格式的抽象基类。
 48      * 抽象类,获取实例:NumberFormat nf = NumberFormat.getInstance();
 49      * 格式化数值:nf.format(number); 解析数值文本:nf.parse(strNumber);
 50      * 主要作用体现在国际化格式规范定义,以及获取简单数值格式,具体格式约束通过其子类:DecimalFormat实现
 51      */
 52     public void testNumberFormat() {
 53         double d = 3.1415926;
 54         int i = 123456;
 55         String s = "3.1415926";
 56         // NumberFormat
 57         NumberFormat nf = NumberFormat.getNumberInstance();// NumberFormat.getInstance()等效
 58         // 添加地区国际化语言
 59         nf.getInstance(Locale.CHINA);// getInstance(Locale.CHINA);
 60         Locale.setDefault(Locale.CHINA);// 设置JVM虚拟机默认语言格式
 61         // 获得整数格式
 62         nf.getIntegerInstance();
 63         // 获得百分数格式
 64         nf.getPercentInstance();
 65         // 获得货币格式
 66         nf.getCurrencyInstance();
 67         // 解析给定字符串开头的文本,生成一个数值
 68         try {
 69             nf.parse(s);
 70         } catch (ParseException e) {
 71             e.printStackTrace();
 72         }
 73         // 设置此格式中是否使用分组
 74         nf.setGroupingUsed(true);
 75         // 设置数的小数部分所允许的最大位数、最小位数,系统默认为三位小数
 76         nf.setMaximumFractionDigits(2);
 77         nf.setMinimumFractionDigits(2);
 78         // 设置正数部分所允许的最大位数、最小位数
 79         nf.setMaximumIntegerDigits(2);
 80         nf.setMinimumIntegerDigits(2);
 81         // 使用上面定义的nf,格式规范数字d
 82         String sd = nf.format(d);
 83     }
 84 
 85     /**
 86     * DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字
 87     * 要获取具体语言环境的 NumberFormat(包括默认语言环境),可调用 NumberFormat 的某个工厂方法,如 getInstance()。
 88     * 通常不直接调用 DecimalFormat 的构造方法,因为 NumberFormat 的工厂方法可能返回不同于 DecimalFormat 的子类。
 89     */
 90     public void testDecimalFormat() {
 91         String pattern = "";
 92         // 使用默认模式和默认语言环境的符号创建一个 DecimalFormat
 93         DecimalFormat df = new DecimalFormat();
 94         // 使用给定的模式和默认语言环境的符号创建一个 DecimalFormat
 95         DecimalFormat df1 = new DecimalFormat(pattern);
 96         // 通过继承获取
 97         DecimalFormat df2 = null;
 98         try {
 99             df2 = (DecimalFormat) NumberFormat.getInstance();
100         } catch (ClassCastException e) {
101             System.out.println(e);
102         }
103         // 为格式化对象添加格式模式
104         df.applyPattern(pattern);
105         // 使用分组,默认为true,并设置分组大小
106         df.setGroupingUsed(true);
107         df.setGroupingSize(3);
108         // 设置正数前、后缀
109         df.setNegativePrefix("+");
110         df.setNegativeSuffix("%");
111         // 设置负数前、后缀
112         df.setPositivePrefix("-");
113         df.setPositiveSuffix("%");
114         // 使用方法控制小数位数、整数位数、货币格式
115         df.setMaximumFractionDigits(2);
116         df.setMinimumIntegerDigits(2);
117         df.setCurrency(Currency.getInstance(Locale.CHINA));
118         // 允许设置整数中小数分隔符的行为。(有小数时始终显示小数分隔符。true时,无小数也显示小数分隔符)
119         df.setDecimalSeparatorAlwaysShown(false);
120         // 设置小数的舍入模式,默认模式为RoundingMode.HALF_EVEN
121         df.setRoundingMode(RoundingMode.HALF_EVEN);
122         /*
123          * pattern的形式举例: 1、
124          */
125     }
原文地址:https://www.cnblogs.com/zi-yao/p/6250790.html