DecimalFormat -- 数字格式操纵类

  1. /*
  2. 数字格式操纵类   
  3. */
  4.    
  5. //返回List
  6. list=DbUtil.getSqlMapClient().queryForList("DeliverIntimeRateManage.select.byQuery",condition);
  7. //返回记录数字符串
  8. indexCount=(String) DbUtil.getSqlMapClient().queryForObject("DeliverIntimeRateManage.select.byQueryReturnCount",condition).toString();
  9. if(Integer.valueOf(indexCount)!=0){
  10.     Iterator<DeliverBean> it = list.iterator();
  11.     while(it.hasNext()){
  12.         DeliverBean bean = it.next();
  13.         DecimalFormat df=new DecimalFormat("00.00%");//添加百分比格式,返回字符串
  14.         bean.setPercent(df.format(bean.getN_deliverintimerate()));
  15.     }
  16. }

网上的例外一个例子:
  1. import java.text.*;
  2. import java.util.*;
  3. public class DecimalFormatSample {
  4.  public static void main(String args[]) {
  5.   DecimalFormat myformat1 = new DecimalFormat("###,###.0000");//使用系统默认的格式
  6.   System.out.println(myformat1.format(111111123456.12));
  7.   Locale.setDefault(Locale.US);
  8.   DecimalFormat myformat2 = new DecimalFormat("###,###.0000");//使用美国的格式
  9.   System.out.println(myformat2.format(111111123456.12));
  10.   //----------------------------also use applypattern------------------------------//
  11.   DecimalFormat myformat3 = new DecimalFormat();
  12.   myformat3.applyPattern("##,###.000");
  13.   System.out.println(myformat3.format(11112345.12345));
  14. //-----------------控制指数输出-------------------------------------------------//
  15.      DecimalFormat myformat4 = new DecimalFormat();
  16.   myformat4.applyPattern("0.000E0000");
  17.   System.out.println(myformat4.format(10000));
  18.   System.out.println(myformat4.format(12345678.345));
  19. //------------------百分数的输出-------------------------------------------//
  20. /*     DecimalFormat是NumberFormat的一个子类,其实例被指定为特定的地区。因此,你可以使用NumberFormat.getInstance 指定一个地区,
  21. 然后将结构强制转换为一个DecimalFormat对象。文档中提到这个技术可以在大多情况下适用,但是你需要用try/catch 块包围强制转换以防转
  22. 换不能正常工作 (大概在非常不明显得情况下使用一个奇异的地区)。    */
  23.        DecimalFormat myformat5 = null;
  24.   try{
  25.       myformat5 = (DecimalFormat)NumberFormat.getPercentInstance();
  26.   }catch(ClassCastException e)
  27.   {
  28.    System.err.println(e); 
  29.   }
  30.   myformat5.applyPattern("00.0000%");
  31.   System.out.println(myformat5.format(0.34567));
  32.   System.out.println(myformat5.format(1.34567));
  33.       
  34.  
  35.  }
  36. /*---------------------------------运行结果-------------------------------------------//
  37.  F:/2004-04-12>java DecimalFormatSample
  38. 111,111,123,456.1200
  39. 111,111,123,456.1200
  40. 11,112,345.123
  41. 1.000E0004
  42. 1.235E0007
  43. 34.5670%
  44. 134.5670%
  45. */
  46. }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值