用 DecimalFormat 格式化数字

用 DecimalFormat 格式化数字

引言
Java中对浮点数的输出表示
在Java中浮点数包括基本型float、double,以及对象包装类型的Float和Double,对于这些浮点数的输出,不管是显式地还是隐式地调用toString()得到它的表示字串,输出格式都是按照如下规则进行的
如果绝对值大于0.001、小于10000000,那么就以常规的小数形式表示。   
如果在上述范围之外,则使用科学计数法表示。即类似于1.234E8的形式。

可以使用 java.text.DecimalFormat及其父类NumberFormat格式化数字
本例只浅述DecimalFormat的使用。

Pattern
0 - 如果对应位置上没有数字,则用零代替
# - 如果对应位置上没有数字,则保持原样(不用补);如果最前、后为0,则保持为空。
正负数模板用分号(;)分割

Number Format Pattern Syntax
You can design your own format patterns for numbers by following the rules specified by the following BNF diagram:
pattern    := subpattern{;subpattern}
subpattern := {prefix}integer{.fraction}{suffix}
prefix     := '//u0000'..'//uFFFD' - specialCharacters
suffix     := '//u0000'..'//uFFFD' - specialCharacters
integer    := '#'* '0'* '0'
fraction   := '0'* '#'*

DEMO
value
123456.789

pattern
,###.###

output
123,456.789

Explanation
The pound sign (#) denotes a digit,
the comma(逗号) is a placeholder for the grouping separator,
and the period(句号) is a placeholder for the decimal separator.
井号(#)表示一位数字,逗号是用于分组分隔符的占位符,点是小数点的占位符。
如果小数点的右面,值有三位,但是式样只有两位。format方法通过四舍五入处理。

value
123.78

pattern
000000.000

output
000123.780

Explanation
The pattern specifies leading and trailing zeros,
because the 0 character is used instead of the pound sign (#).

应用实例 1:
/* * Copyright (c) 1995-1998 Sun Microsystems, Inc. All Rights Reserved. */
import java.util.*;
import java.text.*;
public class DecimalFormatDemo {
 static public void customFormat(String pattern, double value ) {
  DecimalFormat myFormatter = new DecimalFormat(pattern);
  String output = myFormatter.format(value);
  System.out.println(value + " " + pattern + " " + output);
 }
 static public void localizedFormat(String pattern, double value, Locale loc ) {
  NumberFormat nf = NumberFormat.getNumberInstance(loc);
  DecimalFormat df = (DecimalFormat)nf;
  df.applyPattern(pattern);
  String output = df.format(value);
  System.out.println(pattern + " " + output + " " + loc.toString());
 }
 static public void main(String[] args) {
  customFormat("###,###.###", 123456.789);
  customFormat("###.##", 123456.789);
  customFormat("000000.000", 123.78);
  customFormat("$###,###.###", 12345.67);
  customFormat("/u00a5###,###.###", 12345.67);
  Locale currentLocale = new Locale("en", "US");
  DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(currentLocale);
  unusualSymbols.setDecimalSeparator('|');
  unusualSymbols.setGroupingSeparator('^');
  String strange = "#,##0.###";
  DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);
  weirdFormatter.setGroupingSize(4);
  String bizarre = weirdFormatter.format(12345.678);
  System.out.println(bizarre);
  Locale[] locales = { new Locale("en", "US"),
         new Locale("de", "DE"),
         new Locale("fr", "FR") };
  for (int i = 0; i < locales.length; i++) {
   localizedFormat("###,###.###", 123456.789, locales[i]);
  }
 }
}

应用实例 2:
参见:Mark Leung的学习总结
     http://blog.csdn.net/marcoleung/archive/2004/11/11/176514.aspx
public class test
{
    public test()
    {
    //---------------------------------------------
    //定义一个数字格式化对象,格式化模板为".##",即保留2位小数.
    DecimalFormat a = new DecimalFormat("##.##");
    String s= a.format(03.03035);
    System.err.println(s);
    String s= a.format(03.30);
    System.err.println(s);
    String s= a.format(03.335);
    System.err.println(s);
    String s= a.format(03.3);
    System.err.println(s);
    //说明:如果小数点后面不够2位小数,不会补零.参见Rounding小节
    //---------------------------------------------

    //-----------------------------------------------
    //可以在运行时刻用函数applyPattern(String)修改格式模板
    //保留2位小数,如果小数点后面不够2位小数会补零
    a.applyPattern("00.00");
    s = a.format(333.3);
    System.err.println(s);
    //------------------------------------------------

    //------------------------------------------------
    //添加千分号
    a.applyPattern(".##/u2030");
    s = a.format(0.78934);
    System.err.println(s);//添加千位符后,小数会进三位并加上千位符
    //------------------------------------------------

    //------------------------------------------------
    //添加百分号
    a.applyPattern("#.##%");
    s = a.format(0.78645);
    System.err.println(s);
    //------------------------------------------------

   //------------------------------------------------
    //添加前、后修饰字符串,记得要用单引号括起来
    a.applyPattern("'这是我的钱$',###.###'美圆'");
    s = a.format(2533333443.3333);
    System.err.println(s);
    //------------------------------------------------

     //------------------------------------------------
    //添加货币表示符号(不同的国家,添加的符号不一样
    a.applyPattern("/u00A4");
    s = a.format(34);
    System.err.println(s);
    //------------------------------------------------

    //-----------------------------------------------
    //定义正负数模板,记得要用分号隔开
     a.applyPattern("0.0;'@'-#.0");
     s = a.format(33);
     System.err.println(s);
     s = a.format(-33);
     System.err.println(s);
     //-----------------------------------------------

    //综合运用,正负数的不同前后缀
    String pattern="'my moneny'###,###.##'RMB';'ur money'###,###.##'US'";
    a.applyPattern(pattern);
    System.out.println(a.format(-1223233.456));
  }


    public static void main(String[] args)
    {
        test test1 = new test();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值