1. 程式人生 > >貨幣顯示工具類:會計格式,非科學計數法等

貨幣顯示工具類:會計格式,非科學計數法等

package chanson;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @title 貨幣顯示處理工具類
 * @detail 包含以下內容:
 * 1、四捨五入求值
 * 2、針對不同的格式化要求:萬,百萬,億等
 * 3、會計格式的貨幣值:新增','符號
 * 4、非科學計數法的貨幣值
 * @author chanson
 * @time 2008-12-9
 * @version 1.0
 */
public class MoneyUtil {
	
	/**
	 * @title 獲取格式化的人民幣(四捨五入)
	 * @author chanson
	 * @param money  待處理的人民幣
	 * @param scale  小數點後保留的位數
	 * @param divisor 格式化值(萬,百萬,億等等)
	 * @return
	 */
	public String getFormatMoney(double money, int scale, double divisor){
		if(divisor == 0){return "0.00";}
		if(scale < 0){return "0.00";}
		BigDecimal moneyBD = new BigDecimal(money);
		BigDecimal divisorBD = new BigDecimal(divisor);
		//RoundingMode.HALF_UP = 2
		return moneyBD.divide(divisorBD, scale, RoundingMode.HALF_UP).toString();
	}
	
	/**
	 * @title 獲取會計格式的人民幣(四捨五入)——新增會計標識:','
	 * @author chanson
	 * @param money  待處理的人民幣
	 * @param scale  小數點後保留的位數
	 * @param divisor 格式化值(萬,百萬,億等等)
	 * @return
	 */
	public String getAccountantMoney(double money, int scale, double divisor){
		String disposeMoneyStr = getFormatMoney(money, scale, divisor);
		//小數點處理
		int dotPosition = disposeMoneyStr.indexOf(".");
		String exceptDotMoeny = null;//小數點之前的字串
		String dotMeony = null;//小數點之後的字串
		if(dotPosition > 0){
			exceptDotMoeny = disposeMoneyStr.substring(0,dotPosition);
			dotMeony = disposeMoneyStr.substring(dotPosition);
		}else{
			exceptDotMoeny = disposeMoneyStr;
		}
		//負數處理
		int negativePosition = exceptDotMoeny.indexOf("-");
		if(negativePosition == 0){
			exceptDotMoeny = exceptDotMoeny.substring(1);
		}
		StringBuffer reverseExceptDotMoney = new StringBuffer(exceptDotMoeny);
		reverseExceptDotMoney.reverse();//字串倒轉
//		reverse(reverseExceptDotMoeny);
		char[] moneyChar = reverseExceptDotMoney.toString().toCharArray();
		StringBuffer returnMeony = new StringBuffer();//返回值
		for(int i = 0; i < moneyChar.length; i++){
			if(i != 0 && i % 3 == 0){
				returnMeony.append(",");//每隔3位加','
			}
			returnMeony.append(moneyChar[i]);
		}
		returnMeony.reverse();//字串倒轉
//		reverse(returnMeony);
		if(dotPosition > 0){
			returnMeony.append(dotMeony);
		}
		if(negativePosition == 0){
			return "-" + returnMeony.toString();
		}else{
			return returnMeony.toString();
		}
	}
	
	/**
	 * @title 字串倒轉方法
	 * @detail 字串倒轉方法
	 * @author chanson
	 * @param oldStr
	 */
//	 public void reverse(StringBuffer oldStr){
//		  char[] oldStrChar = oldStr.toString().toCharArray();
//		  StringBuffer newStr = new StringBuffer();
//		  for(int i = oldStrChar.length - 1; i > -1; i--){
//			  newStr.append(oldStrChar[i]);
//		  }
//		  oldStr = newStr;
//	  }
	
	public static void main(String[] args) {
		double money = -1269486459.86;
		int scale = 2;
		double divisor = 10000.00;
		System.out.println("原貨幣值: "+money);
		MoneyUtil util = new MoneyUtil();
//		System.out.println("貨幣值: "+util.getAccountantMoney(money, scale, 1));
		String formatMeony = util.getFormatMoney(money, scale, divisor);
		System.out.println("格式化貨幣值: "+formatMeony+"萬元");
		String accountantMoney = util.getAccountantMoney(money, scale, divisor);
		System.out.println("會計貨幣值: "+accountantMoney+"萬元");
	}
}