java.text.DecimalFormat学习笔记


import java.text.*;

public class Untitled1    {
   public static void main(String[] args) {

     //---------------------------------------------
     //定义一个数字格式化对象,格式化模板为".##",即保留2位小数.
     DecimalFormat a = new DecimalFormat(".##");
     String s= a.format(333.335);
     System.err.println(s);
     //说明:如果小数点后面不够2位小数,不会补零.参见Rounding小节
     //---------------------------------------------

     //-----------------------------------------------
     //可以在运行时刻用函数applyPattern(String)修改格式模板
     //保留2位小数,如果小数点后面不够2位小数会补零
     a.applyPattern(".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(33333443.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));
   }
}

总结:
要生成一个DecimalFormat对象,一般只要通过NumberFormat类工厂的getInstance()来取得一个 NumberFormat对象再将其转换成DecimalFormat对象,然后通过DecimalForat对象的applyPattern()来动态改变数据的现示格式模板,通过format()方法取得格式化后的数字。同时,DecimalFormat提供了许多的方法来返回格式化后的数字的某一部份,这些方法如:getNegativeSuffix()。这个类的难点主要是在模板的书写及理解上。其实主要的就是针对一个数字的正负形式来设定不同的格式显示。这里要特别注意的是使用在模板上的特殊字符代表有特殊的意义,如下表所示:
Symbol    Description
0    a digit
#    a digit, zero shows as absent
.    placeholder for decimal separator
,    placeholder for grouping separator
E   separates mantissa and exponent for exponential formats
;    separates formats
-    default negative prefix
%    multiply by 100 and show as percentage
?    multiply by 1000 and show as per mille
¤    currency sign; replaced by currency symbol; if doubled, replaced by international currency symbol; if present in a pattern, the monetary decimal separator is used instead of the decimal separator  
X    any other characters can be used in the prefix or suffix
'    used to quote special characters in a prefix or suffix

例如:如果模板中含有#,意思是指这个#号可代表一个或多个数字如果该位的数字是零的话则省略该位。另:注意“#,##0.0#;(#)”这个模板的意思是指数字的负数形式跟正数的一样。

转载于:https://my.oschina.net/willSoft/blog/100486

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值