---
title: "java数字格式化"
author: "Java-Object"
date: "2014-03-25T13:04:40Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2014-03-25T13:04:40Z"
id: 2036311
url: "https://hezd.iteye.com/blog/2036311"
---

Java数字的格式化 ： 

Java代码 [![复制代码](http://jerval.iteye.com/images/icon_copy.gif)](http://jerval.iteye.com/blog/874983 "复制代码") [![收藏代码](http://jerval.iteye.com/images/icon_star.png)![](http://jerval.iteye.com/images/spinner.gif)](http://blog.csdn.net/zhengbo0/article/details/6967601 "收藏这段代码")

01. double pi = 3.1415927;//pi
02. // 取一位整数
03. System.out.println(new DecimalFormat("0").format(pi));// 3
04. // 取一位整数和两位小数
05. System.out.println(new DecimalFormat("0.00").format(pi));// 3.14
06. // 取两位整数和三位小数，整数不足部分以0填补。
07. System.out.println(new DecimalFormat("00.000").format(pi));// 03.142
08. // 取所有整数部分
09. System.out.println(new DecimalFormat("#").format(pi));// 3
10. // 以百分比方式计数，并取两位小数
11. System.out.println(new DecimalFormat("#.##%").format(pi));// 314.16%
12. long c = 299792458;
13. // 显示为科学计数法，并取五位小数
14. System.out.println(new DecimalFormat("#.#####E0").format(c));// 2.99792E8
15. // 显示为两位整数的科学计数法，并取四位小数
16. System.out.println(new DecimalFormat("00.####E0").format(c));// 29.9792E7
17. // 每三位以逗号进行分隔。
18. System.out.println(new DecimalFormat(",###").format(c));// 299,792,458
19. // 将格式嵌入文本
20. System.out.println(new DecimalFormat("光速大小为每秒,###米。").format(c));

**\[java]** [view plain](http://blog.csdn.net/zhengbo0/article/details/6967601 "view plain")[copy](http://blog.csdn.net/zhengbo0/article/details/6967601 "copy")[print](http://blog.csdn.net/zhengbo0/article/details/6967601 "print")[?](http://blog.csdn.net/zhengbo0/article/details/6967601 "?")

 

01. double pi = 3.1415927;//pi
02. // 取一位整数
03. System.out.println(new DecimalFormat("0").format(pi));// 3
04. // 取一位整数和两位小数
05. System.out.println(new DecimalFormat("0.00").format(pi));// 3.14
06. // 取两位整数和三位小数，整数不足部分以0填补。
07. System.out.println(new DecimalFormat("00.000").format(pi));// 03.142
08. // 取所有整数部分
09. System.out.println(new DecimalFormat("#").format(pi));// 3
10. // 以百分比方式计数，并取两位小数
11. System.out.println(new DecimalFormat("#.##%").format(pi));// 314.16%
12. long c = 299792458;
13. // 显示为科学计数法，并取五位小数
14. System.out.println(new DecimalFormat("#.#####E0").format(c));// 2.99792E8
15. // 显示为两位整数的科学计数法，并取四位小数
16. System.out.println(new DecimalFormat("00.####E0").format(c));// 29.9792E7
17. // 每三位以逗号进行分隔。
18. System.out.println(new DecimalFormat(",###").format(c));// 299,792,458
19. // 将格式嵌入文本
20. System.out.println(new DecimalFormat("光速大小为每秒,###米。").format(c));

----------------------------------------------------------------------   
DecimalFormat 类主要靠 # 和 0 两种占位符号来指定数字长度。0 表示如果位数不足则以 0 填充，# 表示只要有可能就把数字拉上这个位置。   
有时我们需要控制输出的数字的格式，如何使用java的类库做到这个呢？   
也许你不关心格式，但是你需要关心你的程序可以在全世界通用，像下面的这样一个简单的语句是依赖地区的：   
    System.out.println(1234.56);   
在美国，"." 是小数点，但在其它地方就不一定了。如何处理这个呢？   
java.text 包中的一些包可以处理这类问题。下面的简单范例使用那些类解决上面提出的问题： 

Java代码 [![复制代码](http://jerval.iteye.com/images/icon_copy.gif)](http://jerval.iteye.com/blog/874983 "复制代码") [![收藏代码](http://jerval.iteye.com/images/icon_star.png)![](http://jerval.iteye.com/images/spinner.gif)](http://blog.csdn.net/zhengbo0/article/details/6967601 "收藏这段代码")

01. import java.text.NumberFormat;
02. import java.util.Locale;
03. public class DecimalFormat1 {
04. public static void main(String args\[]) {
05. // 得到本地的缺省格式
06. NumberFormat nf1 = NumberFormat.getInstance();
07. System.out.println(nf1.format(1234.56));
08. // 得到德国的格式
09. NumberFormat nf2 =
10. NumberFormat.getInstance(Locale.GERMAN);
11. System.out.println(nf2.format(1234.56));
12. }
13. }

**\[java]** [view plain](http://blog.csdn.net/zhengbo0/article/details/6967601 "view plain")[copy](http://blog.csdn.net/zhengbo0/article/details/6967601 "copy")[print](http://blog.csdn.net/zhengbo0/article/details/6967601 "print")[?](http://blog.csdn.net/zhengbo0/article/details/6967601 "?")

 

01. import java.text.NumberFormat;
02. import java.util.Locale;
03. public class DecimalFormat1 {
04. public static void main(String args\[]) {
05. // 得到本地的缺省格式
06. NumberFormat nf1 = NumberFormat.getInstance();
07. System.out.println(nf1.format(1234.56));
08. // 得到德国的格式
09. NumberFormat nf2 =
10. NumberFormat.getInstance(Locale.GERMAN);
11. System.out.println(nf2.format(1234.56));
12. }
13. }

如果你在美国，运行程序后输出：   
    1,234.56   
    1.234,56   
换句话说，在不同的地方使用不同的习惯表示数字。   
NumberFormat.getInstance()方法返回NumberFormat的一个实例(实际上是NumberFormat具体的一个子类，例如DecimalFormat), 这适合根据本地设置格式化一个数字。你也可以使用非缺省的地区设置，例如德国。然后格式化方法根据特定的地区规则格式化数字。这个程序也可以使用一个简单的形式：   
    NumberFormat.getInstance().format(1234.56)   
但是保存一个格式然后重用更加有效。国际化是格式化数字时的一个大问题。   
另一个是对格式的有效控制，例如指定小数部分的位数，下面是解决这个问题的一个简单例子： 

Java代码 [![复制代码](http://jerval.iteye.com/images/icon_copy.gif)](http://jerval.iteye.com/blog/874983 "复制代码") [![收藏代码](http://jerval.iteye.com/images/icon_star.png)![](http://jerval.iteye.com/images/spinner.gif)](http://blog.csdn.net/zhengbo0/article/details/6967601 "收藏这段代码")

01. import java.text.DecimalFormat;
02. import java.util.Locale;
03. public class DecimalFormat2 {
04. public static void main(String args\[]) {
05. // 得到本地的缺省格式
06. DecimalFormat df1 = new DecimalFormat("####.000");
07. System.out.println(df1.format(1234.56));
08. // 得到德国的格式
09. Locale.setDefault(Locale.GERMAN);
10. DecimalFormat df2 = new DecimalFormat("####.000");
11. System.out.println(df2.format(1234.56));
12. }
13. }

**\[java]** [view plain](http://blog.csdn.net/zhengbo0/article/details/6967601 "view plain")[copy](http://blog.csdn.net/zhengbo0/article/details/6967601 "copy")[print](http://blog.csdn.net/zhengbo0/article/details/6967601 "print")[?](http://blog.csdn.net/zhengbo0/article/details/6967601 "?")

 

01. import java.text.DecimalFormat;
02. import java.util.Locale;
03. public class DecimalFormat2 {
04. public static void main(String args\[]) {
05. // 得到本地的缺省格式
06. DecimalFormat df1 = new DecimalFormat("####.000");
07. System.out.println(df1.format(1234.56));
08. // 得到德国的格式
09. Locale.setDefault(Locale.GERMAN);
10. DecimalFormat df2 = new DecimalFormat("####.000");
11. System.out.println(df2.format(1234.56));
12. }
13. }

在这个例子中设置了数字的格式，使用像"####.000"的符号。这个模式意味着在小数点前有四个数字，如果不够就空着，小数点后有三位数字，不足用0补齐。程序的输出：   
    1234.560   
    1234,560   
相似的，也可以控制指数形式的格式，例如：   
 

Java代码 [![复制代码](http://jerval.iteye.com/images/icon_copy.gif)](http://jerval.iteye.com/blog/874983 "复制代码") [![收藏代码](http://jerval.iteye.com/images/icon_star.png)![](http://jerval.iteye.com/images/spinner.gif)](http://blog.csdn.net/zhengbo0/article/details/6967601 "收藏这段代码")

1. import java.text.DecimalFormat;
2. public class DecimalFormat3 {
3. public static void main(String args\[]) {
4. DecimalFormat df = new DecimalFormat("0.000E0000");
5. System.out.println(df.format(1234.56));
6. }
7. }

**\[java]** [view plain](http://blog.csdn.net/zhengbo0/article/details/6967601 "view plain")[copy](http://blog.csdn.net/zhengbo0/article/details/6967601 "copy")[print](http://blog.csdn.net/zhengbo0/article/details/6967601 "print")[?](http://blog.csdn.net/zhengbo0/article/details/6967601 "?")

 

1. import java.text.DecimalFormat;
2. public class DecimalFormat3 {
3. public static void main(String args\[]) {
4. DecimalFormat df = new DecimalFormat("0.000E0000");
5. System.out.println(df.format(1234.56));
6. }
7. }

输出：   
    1.235E0003   
对于百分数： 

Java代码 [![复制代码](http://jerval.iteye.com/images/icon_copy.gif)](http://jerval.iteye.com/blog/874983 "复制代码") [![收藏代码](http://jerval.iteye.com/images/icon_star.png)![](http://jerval.iteye.com/images/spinner.gif)](http://blog.csdn.net/zhengbo0/article/details/6967601 "收藏这段代码")

1. import java.text.NumberFormat;
2. public class DecimalFormat4 {
3. public static void main(String args\[]) {
4. NumberFormat nf = NumberFormat.getPercentInstance();
5. System.out.println(nf.format(0.47));
6. }
7. }

**\[java]** [view plain](http://blog.csdn.net/zhengbo0/article/details/6967601 "view plain")[copy](http://blog.csdn.net/zhengbo0/article/details/6967601 "copy")[print](http://blog.csdn.net/zhengbo0/article/details/6967601 "print")[?](http://blog.csdn.net/zhengbo0/article/details/6967601 "?")

 

1. import java.text.NumberFormat;
2. public class DecimalFormat4 {
3. public static void main(String args\[]) {
4. NumberFormat nf = NumberFormat.getPercentInstance();
5. System.out.println(nf.format(0.47));
6. }
7. }

输出：   
    47%   
至此，你已经看到了格式化数字的几个不同的技术。另一方面，如何读取并解析包含格式化的数字的字符串？解析支持包含在NumberFormat中。例如： 

Java代码 [![复制代码](http://jerval.iteye.com/images/icon_copy.gif)](http://jerval.iteye.com/blog/874983 "复制代码") [![收藏代码](http://jerval.iteye.com/images/icon_star.png)![](http://jerval.iteye.com/images/spinner.gif)](http://blog.csdn.net/zhengbo0/article/details/6967601 "收藏这段代码")

01. import java.util.Locale;
02. import java.text.NumberFormat;
03. import java.text.ParseException;
04. public class DecimalFormat5 {
05. public static void main(String args\[]) {
06. // 本地格式
07. NumberFormat nf1 = NumberFormat.getInstance();
08. Object obj1 = null;
09. // 基于格式的解析
10. try {
11. obj1 = nf1.parse("1234,56");
12. }
13. catch (ParseException e1) {
14. System.err.println(e1);
15. }
16. System.out.println(obj1);
17. // 德国格式
18. NumberFormat nf2 =
19. NumberFormat.getInstance(Locale.GERMAN);
20. Object obj2 = null;
21. // 基于格式的解析
22. try {
23. obj2 = nf2.parse("1234,56");
24. }
25. catch (ParseException e2) {
26. System.err.println(e2);
27. }
28. System.out.println(obj2);
29. }
30. }

**\[java]** [view plain](http://blog.csdn.net/zhengbo0/article/details/6967601 "view plain")[copy](http://blog.csdn.net/zhengbo0/article/details/6967601 "copy")[print](http://blog.csdn.net/zhengbo0/article/details/6967601 "print")[?](http://blog.csdn.net/zhengbo0/article/details/6967601 "?")

 

01. import java.util.Locale;
02. import java.text.NumberFormat;
03. import java.text.ParseException;
04. public class DecimalFormat5 {
05. public static void main(String args\[]) {
06. // 本地格式
07. NumberFormat nf1 = NumberFormat.getInstance();
08. Object obj1 = null;
09. // 基于格式的解析
10. try {
11. obj1 = nf1.parse("1234,56");
12. }
13. catch (ParseException e1) {
14. System.err.println(e1);
15. }
16. System.out.println(obj1);
17. // 德国格式
18. NumberFormat nf2 =
19. NumberFormat.getInstance(Locale.GERMAN);
20. Object obj2 = null;
21. // 基于格式的解析
22. try {
23. obj2 = nf2.parse("1234,56");
24. }
25. catch (ParseException e2) {
26. System.err.println(e2);
27. }
28. System.out.println(obj2);
29. }
30. }

这个例子分两部分，都是解析一个字符串："1234,56"。第一部分使用本地格式解析，第二部分使用德国格式解析。当程序在美国运行，结果是：   
    123456   
    1234.56   
换句话说，"1234,56"在美国被认为是一个巨大的整数"123456"而在德国被认为是一个小数"1234.56"。   
还有格式化讨论的最后一个问题。在上面的例子中， DecimalFormat 和 NumberFormat 都被使用了。DecimalFormat 常用于获得很好的格式控制，而NumberFormat 常用于指定不同于本地的地区。如何结合两个类呢？   
答案围绕着这样的事实：DecimalFormat是NumberFormat的一个子类,其实例被指定为特定的地区。因此，你可以使用NumberFormat.getInstance 指定一个地区，然后将结构强制转换为一个DecimalFormat对象。文档中提到这个技术可以在大多情况下适用，但是你需要用try/catch 块包围强制转换以防转换不能正常工作 (大概在非常不明显得情况下使用一个奇异的地区)。下面是一个这样的例子：   
 

Java代码 [![复制代码](http://jerval.iteye.com/images/icon_copy.gif)](http://jerval.iteye.com/blog/874983 "复制代码") [![收藏代码](http://jerval.iteye.com/images/icon_star.png)![](http://jerval.iteye.com/images/spinner.gif)](http://blog.csdn.net/zhengbo0/article/details/6967601 "收藏这段代码")

01. import java.text.DecimalFormat;
02. import java.text.NumberFormat;
03. import java.util.Locale;
04. public class DecimalFormat6 {
05. public static void main(String args\[]) {
06. DecimalFormat df = null;
07. // 得到一个NumberFormat 对象并
08. // 强制转换为一个 DecimalFormat 对象
09. try {
10. df = (DecimalFormat)
11. NumberFormat.getInstance(Locale.GERMAN);
12. }
13. catch (ClassCastException e) {
14. System.err.println(e);
15. }
16. // 设置格式模式
17. df.applyPattern("####.00000");
18. // format a number
19. System.out.println(df.format(1234.56));
20. }
21. }

**\[java]** [view plain](http://blog.csdn.net/zhengbo0/article/details/6967601 "view plain")[copy](http://blog.csdn.net/zhengbo0/article/details/6967601 "copy")[print](http://blog.csdn.net/zhengbo0/article/details/6967601 "print")[?](http://blog.csdn.net/zhengbo0/article/details/6967601 "?")

 

01. import java.text.DecimalFormat;
02. import java.text.NumberFormat;
03. import java.util.Locale;
04. public class DecimalFormat6 {
05. public static void main(String args\[]) {
06. DecimalFormat df = null;
07. // 得到一个NumberFormat 对象并
08. // 强制转换为一个 DecimalFormat 对象
09. try {
10. df = (DecimalFormat)
11. NumberFormat.getInstance(Locale.GERMAN);
12. }
13. catch (ClassCastException e) {
14. System.err.println(e);
15. }
16. // 设置格式模式
17. df.applyPattern("####.00000");
18. // format a number
19. System.out.println(df.format(1234.56));
20. }
21. }

getInstance() 方法获得格式，然后调用applyPattern()方法设置格式模式，输出：   
    1234,56000   
如果你不关心国际化，可以直接使用DecimalFormat 

Java代码 [![复制代码](http://jerval.iteye.com/images/icon_copy.gif)](http://jerval.iteye.com/blog/874983 "复制代码") [![收藏代码](http://jerval.iteye.com/images/icon_star.png)![](http://jerval.iteye.com/images/spinner.gif)](http://blog.csdn.net/zhengbo0/article/details/6967601 "收藏这段代码")

01. DecimalFormat df1 = new DecimalFormat("###.00");
02. System.out.println(df1.format(234234.234634));
03. System.out.println(df1.format(34.234634));
04. DecimalFormat df2 = new DecimalFormat("0.00E0000");
05. System.out.println(df2.format(23423.34234234));
06. DecimalFormat df3 = (DecimalFormat)NumberFormat.getInstance(Locale.CHINESE);
07. df3.applyPattern("####.000");
08. System.out.println(df3.format(23423.34234234));
09. df3.applyPattern("00.0000%");
10. System.out.println(df3.format(0.5552445));
11. NumberFormat nf1 = NumberFormat.getInstance();
12. System.out.println(nf1.format(13423423.234234));
13. NumberFormat nf2 = NumberFormat.getPercentInstance();
14. System.out.println(nf2.format(0.55));

**\[java]** [view plain](http://blog.csdn.net/zhengbo0/article/details/6967601 "view plain")[copy](http://blog.csdn.net/zhengbo0/article/details/6967601 "copy")[print](http://blog.csdn.net/zhengbo0/article/details/6967601 "print")[?](http://blog.csdn.net/zhengbo0/article/details/6967601 "?")

 

01. DecimalFormat df1 = new DecimalFormat("###.00");
02. System.out.println(df1.format(234234.234634));
03. System.out.println(df1.format(34.234634));
04. DecimalFormat df2 = new DecimalFormat("0.00E0000");
05. System.out.println(df2.format(23423.34234234));
06. DecimalFormat df3 = (DecimalFormat)NumberFormat.getInstance(Locale.CHINESE);
07. df3.applyPattern("####.000");
08. System.out.println(df3.format(23423.34234234));
09. df3.applyPattern("00.0000%");
10. System.out.println(df3.format(0.5552445));
11. NumberFormat nf1 = NumberFormat.getInstance();
12. System.out.println(nf1.format(13423423.234234));
13. NumberFormat nf2 = NumberFormat.getPercentInstance();
14. System.out.println(nf2.format(0.55));

结果：   
234234.23   
34.23   
2.34E0004   
23423.342   
55.5244%   
13,423,423.234   
55% 

如果要将带一定格式的字符串数字转化为数字类型，则值需要调用格式对象的parse()方法即可

 

转载自：[http://blog.csdn.net/zhengbo0/article/details/6967601](http://blog.csdn.net/zhengbo0/article/details/6967601)