金融类金额转换

package util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.List;

/**
 * 金融类转换
 * @author Administrator
 *
 */
public class TradePortalUtil {
	/***
	 * 将list转为数组
	 * 
	 * @param list
	 * @return
	 */
	public static String[] ListToArray(List<String> list) {
		String[] array = new String[list.size()];
		list.toArray(array);
		return array;
	}

	/**
	 * 处理金额,保留两位小数,并且整数位每三位用逗号分开
	 * 
	 * @param money
	 * @return
	 */
	public static String splitMoney(double money) {
		DecimalFormat df = new DecimalFormat("###,###,###.##");
		df.applyPattern("#,##0.00#");
		double rtnVal = Double.parseDouble(String.valueOf(money));
		return df.format(rtnVal);
	}

	/**
	 * 还原金额
	 * 
	 * @param money
	 * @return
	 * @throws ParseException
	 */
	public static double restoreMoney(String money) throws ParseException	 {
		DecimalFormat df = new DecimalFormat("###,###,###.##");
		return df.parse(money).doubleValue();
	}
	
	/**
     * 金额元转分
     * @see 注意:该方法可处理贰仟万以内的金额,且若有小数位,则不限小数位的长度
     * @see 注意:如果你的金额达到了贰仟万以上,则不推荐使用该方法,否则计算出来的结果会令人大吃一惊
     * @param amount  金额的元进制字符串
     * @return String 金额的分进制字符串
     */
    public static String moneyYuanToFen(String amount){
        if(isEmpty(amount)){
            return amount;
        }
        //传入的金额字符串代表的是一个整数
        if(-1 == amount.indexOf(".")){
            return Integer.parseInt(amount) * 100 + "";
        }
        //传入的金额字符串里面含小数点-->取小数点前面的字符串,并将之转换成单位为分的整数表示
        int money_fen = Integer.parseInt(amount.substring(0, amount.indexOf("."))) * 100;
        //取到小数点后面的字符串
        String pointBehind = (amount.substring(amount.indexOf(".") + 1));
        //amount=12.3
        if(pointBehind.length() == 1){
            return money_fen + Integer.parseInt(pointBehind)*10 + "";
        }
        //小数点后面的第一位字符串的整数表示
        int pointString_1 = Integer.parseInt(pointBehind.substring(0, 1));
        //小数点后面的第二位字符串的整数表示
        int pointString_2 = Integer.parseInt(pointBehind.substring(1, 2));
        //amount==12.03,amount=12.00,amount=12.30
        if(pointString_1 == 0){
            return money_fen + pointString_2 + "";
        }else{
            return money_fen + pointString_1*10 + pointString_2 + "";
        }
    }
    /**
     * 判断输入的字符串参数是否为空
     * @return boolean 空则返回true,非空则flase
     */
    public static boolean isEmpty(String input) {
        return null==input || 0==input.length() || 0==input.replaceAll("\\s", "").length();
    }
    
    /**
     * 金额分转元
     * @see 注意:如果传入的参数中含小数点,则直接原样返回
     * @see 该方法返回的金额字符串格式为<code>00.00</code>,其整数位有且至少有一个,小数位有且长度固定为2
     * @param amount  金额的分进制字符串
     * @return String 金额的元进制字符串
     */
    public static String moneyFenToYuan(String amount){
        if(isEmpty(amount)){
            return amount;
        }
        if(amount.indexOf(".") > -1){
            return amount;
        }
        if(amount.length() == 1){
            return "0.0" + amount;
        }else if(amount.length() == 2){
            return "0." + amount;
        }else{
            return amount.substring(0, amount.length()-2) + "." + amount.substring(amount.length()-2);
        }
    }
    

    

}

猜你喜欢

转载自erhuo.iteye.com/blog/2234359