1. 程式人生 > >JAVA數字千分位和小數點的現實(處理金額問題)

JAVA數字千分位和小數點的現實(處理金額問題)

金融類等專案通常對於金額較大的欄位,通常要求千分位顯示,數字保留兩位小數,分裝工具類方便以後工作需要:

-----------------------------------------------------------★菜鳥筆記,如有問題還望留下您寶貴的意見★-------------------------------------------------------------------

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


說明:

1、井號(#)表示一位數字,逗號是用於分組分隔符的佔位符,點是小數點的佔位符。 
2、如果小數點的右面,值有三位,但是式樣只有兩位。format方法通過四捨五入處理。

3、0 - 如果對應位置上沒有數字,則用零代替
4、# - 如果對應位置上沒有數字,則保持原樣(不用補);如果最前、後為0,則保持為空。
5、正負數模板用分號(;)分割

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

方法一:

package com.mo.util;

import java.text.DecimalFormat;

/**
 * @ClassName: FmtMicrometer
 * @Description: 格式化數字為千分位工具類
 * @author wsq	E-mail:
 * @date 2017-6-1 下午02:25:57
 * 
 */
public class FmtMicrometer {
	
	/**
	 * @Title: fmtMicrometer
	 * @Description: 格式化數字為千分位
	 * @param text
	 * @return    設定檔案
	 * @return String    返回型別
	 */
	public static String fmtMicrometer(String text) {
		DecimalFormat df = null;
		if (text.indexOf(".") > 0) {
			if (text.length() - text.indexOf(".") - 1 == 0) {
				df = new DecimalFormat("###,##0.");
			} else if (text.length() - text.indexOf(".") - 1 == 1) {
				df = new DecimalFormat("###,##0.0");
			} else {
				df = new DecimalFormat("###,##0.00");
			}
		} else {
			df = new DecimalFormat("###,##0");
		}
		double number = 0.0;
		try {
			number = Double.parseDouble(text);
		} catch (Exception e) {
			number = 0.0;
		}
		return df.format(number);
	}
	
}


在實體類中使用方法:Bean類

package com.mo.test;

import com.mo.util.FmtMicrometer;

/**
 * @ClassName: QueryXXDao
 * @Description: XX查詢Bean類
 * @author wsq	E-mail:
 * @date 2017-6-1 下午04:15:10
 * 
 */
public class QueryXXDao {
	//其他欄位省略
	private String money;

	public String getMoney() {
		return FmtMicrometer.fmtMicrometer(money);
	}

	public void setMoney(String money) {
		this.money = FmtMicrometer.fmtMicrometer(money);
	}

	@Override
	public String toString() {
		return "QueryXXDao [money=" + money + ", getMoney()=" + getMoney()
				+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
				+ ", toString()=" + super.toString() + "]";
	}
	
	
}


使用時,直接呼叫方法就即可

================================================邪惡分隔符======================================================

方法二:

不推薦此方法,小數的話存在精度問題,也可自行封裝方法處理,自己在main方法中測試了下

	public static void main(String[] args) throws ParseException {
		
		NumberFormat numberFormat1 = NumberFormat.getNumberInstance();
		System.out.println(numberFormat1.format(11122.33)); //結果是11,122.33
		
		NumberFormat numberFormat2 = NumberFormat.getNumberInstance();
		System.out.println(numberFormat2.format(11122.00)); //結果是11,122

		NumberFormat numberFormat3 = NumberFormat.getNumberInstance();
		numberFormat3.setGroupingUsed(false); //設定了以後不會有千分位,如果不設定,預設是有的
		System.out.println(numberFormat3.format(11122.33)); //結果是11122.33 

		//將一個可能包含千分位的數字轉換為不含千分位的形式:
		String amount1 = "13,000.00";
		double d1 = new DecimalFormat().parse(amount1).doubleValue(); //這裡使用的是parse,不是format
		System.out.println(String.valueOf(d1)); //結果是13000.0
	}