JAVA数字千分位和小数点的现实(处理金额问题)

金融类等项目通常对于金额较大的字段,通常要求千分位显示,数字保留两位小数,分装工具类方便以后工作需要:

-----------------------------------------------------------★菜鸟笔记,如有问题还望留下您宝贵的意见★-------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

说明:

1、井号(#)表示一位数字,逗号是用于分组分隔符的占位符,点是小数点的占位符。 
2、如果小数点的右面,值有三位,但是式样只有两位。format方法通过四舍五入处理。

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

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

方法一:

[java]  view plain  copy
  1. package com.mo.util;  
  2.   
  3. import java.text.DecimalFormat;  
  4.   
  5. /** 
  6.  * @ClassName: FmtMicrometer 
  7.  * @Description: 格式化数字为千分位工具类 
  8.  * @author wsq  E-mail: 
  9.  * @date 2017-6-1 下午02:25:57 
  10.  *  
  11.  */  
  12. public class FmtMicrometer {  
  13.       
  14.     /** 
  15.      * @Title: fmtMicrometer 
  16.      * @Description: 格式化数字为千分位 
  17.      * @param text 
  18.      * @return    设定文件 
  19.      * @return String    返回类型 
  20.      */  
  21.     public static String fmtMicrometer(String text) {  
  22.         DecimalFormat df = null;  
  23.         if (text.indexOf(".") > 0) {  
  24.             if (text.length() - text.indexOf(".") - 1 == 0) {  
  25.                 df = new DecimalFormat("###,##0.");  
  26.             } else if (text.length() - text.indexOf(".") - 1 == 1) {  
  27.                 df = new DecimalFormat("###,##0.0");  
  28.             } else {  
  29.                 df = new DecimalFormat("###,##0.00");  
  30.             }  
  31.         } else {  
  32.             df = new DecimalFormat("###,##0");  
  33.         }  
  34.         double number = 0.0;  
  35.         try {  
  36.             number = Double.parseDouble(text);  
  37.         } catch (Exception e) {  
  38.             number = 0.0;  
  39.         }  
  40.         return df.format(number);  
  41.     }  
  42.       
  43. }  


在实体类中使用方法:Bean类


[java]  view plain  copy
  1. package com.mo.test;  
  2.   
  3. import com.mo.util.FmtMicrometer;  
  4.   
  5. /** 
  6.  * @ClassName: QueryXXDao 
  7.  * @Description: XX查询Bean类 
  8.  * @author wsq  E-mail: 
  9.  * @date 2017-6-1 下午04:15:10 
  10.  *  
  11.  */  
  12. public class QueryXXDao {  
  13.     //其他字段省略  
  14.     private String money;  
  15.   
  16.     public String getMoney() {  
  17.         return FmtMicrometer.fmtMicrometer(money);  
  18.     }  
  19.   
  20.     public void setMoney(String money) {  
  21.         this.money = FmtMicrometer.fmtMicrometer(money);  
  22.     }  
  23.   
  24.     @Override  
  25.     public String toString() {  
  26.         return "QueryXXDao [money=" + money + ", getMoney()=" + getMoney()  
  27.                 + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()  
  28.                 + ", toString()=" + super.toString() + "]";  
  29.     }  
  30.       
  31.       
  32. }  



使用时,直接调用方法就即可

================================================邪恶分隔符======================================================

方法二:

不推荐此方法,小数的话存在精度问题,也可自行封装方法处理,自己在main方法中测试了下

[java]  view plain  copy
  1. public static void main(String[] args) throws ParseException {  
  2.       
  3.     NumberFormat numberFormat1 = NumberFormat.getNumberInstance();  
  4.     System.out.println(numberFormat1.format(11122.33)); //结果是11,122.33  
  5.       
  6.     NumberFormat numberFormat2 = NumberFormat.getNumberInstance();  
  7.     System.out.println(numberFormat2.format(11122.00)); //结果是11,122  
  8.   
  9.     NumberFormat numberFormat3 = NumberFormat.getNumberInstance();  
  10.     numberFormat3.setGroupingUsed(false); //设置了以后不会有千分位,如果不设置,默认是有的  
  11.     System.out.println(numberFormat3.format(11122.33)); //结果是11122.33   
  12.   
  13.     //将一个可能包含千分位的数字转换为不含千分位的形式:  
  14.     String amount1 = "13,000.00";  
  15.     double d1 = new DecimalFormat().parse(amount1).doubleValue(); //这里使用的是parse,不是format  
  16.     System.out.println(String.valueOf(d1)); //结果是13000.0  
  17. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值