工具类-金钱流量转换类

金钱流量转换类

package com.mytest.TimeUtils;

import java.math.BigDecimal;
import java.text.NumberFormat;

public class MoneyOrFlowUtil {
	/**
	 * 格式化为金额0.00格式
	 * 
	 * @param amount
	 *            金额单位:分
	 * @return
	 */
	public static String fmMoney(long amount) {
		return new BigDecimal(amount).divide(new BigDecimal(100), 2,
				BigDecimal.ROUND_HALF_UP).toString();
	}

	/**
	 * 格式化为金额0.00格式
	 * 
	 * @param amount
	 *            金额单位:厘
	 * @return
	 */
	public static String fmMoneyLi(long amount) {
		return new BigDecimal(amount).divide(new BigDecimal(1000), 2,
				BigDecimal.ROUND_HALF_UP).toString();
	}

	public static String fmMoneyFen(long amount){
		return new BigDecimal(amount).divide(new BigDecimal(100), 2,
				BigDecimal.ROUND_HALF_UP).toString();
	}
	
	/**
	 * 格式化为金额0.00格式
	 * 
	 * @param amount
	 *            金额单位:毫
	 * @return
	 */
	public static String fmMoneyHao(long amount) {
		return new BigDecimal(amount).divide(new BigDecimal(10000), 2,
				BigDecimal.ROUND_HALF_UP).toString();
	}
	
	/**
	 * 格式化为金额0.00格式
	 * 
	 * @param amount
	 *            金额单位:分
	 * @return
	 */
	public static String fmMoney(String amount) {
		return new BigDecimal(amount).divide(new BigDecimal(100), 2,
				BigDecimal.ROUND_HALF_UP).toString();
	}

	/**
	 * 格式化为金额0.00格式
	 * 
	 * @param amount
	 *            金额单位:厘
	 * @return
	 * xxx
	 */
	public static String fmMoneyLi(String amount) {
		return new BigDecimal(amount).divide(new BigDecimal(1000), 2,
				BigDecimal.ROUND_HALF_UP).toString();
	}
	/**
	 * 讲金额由分 转换为 厘
	 * 
	 * @param amount		金额单位:分
	 * 
	 * @return				单位: 厘
	 */
	public static long fmMoneyFromCent2Li(long amount)throws Exception{
		return amount * 10;
	}
	
	/**
	 * 将金额由 元 转换为 分
	 * 
	 * @param amount
	 *            金额单位:元
	 * @return	单位: 分
	 * xxx
	 */
	public static long fmMoneyFromYuan2Cent(String amount) {
		return new BigDecimal(amount).multiply(new BigDecimal(100)).longValue();
	}
	
	/**
	 * 讲金额由 厘 转换为 分
	 * 
	 * @param amount		金额单位:厘
	 * 
	 * @return				单位: 分
	 * @author xxx
	 */
	public static long fmMoneyFromLi2Cent(long amount)throws Exception{
		return new BigDecimal(amount).divide(new BigDecimal(10), 0,
				BigDecimal.ROUND_HALF_UP).longValue();
	}
	/**
	 * 
	* @Function: fmMoneyFromLi2Cent
	* @Description: 该函数的功能描述
	*
	* @param:参数描述
	* @return:返回结果描述
	* @throws:异常描述
	*
	* @version: v1.0.0
	* @date: 2013-2-26 下午10:59:28 
	*
	* Modification History:
	* Date         Author          Version            Description
	*---------------------------------------------------------*
	* 2013-2-26    wangwei          v1.0.0               修改原因
	 */
	public static long fmMoneyFromLi2Cent(String amount)throws Exception{
		return new BigDecimal(amount).divide(new BigDecimal(10), 0,
				BigDecimal.ROUND_HALF_UP).longValue();
	}
	/**
	 * 从金额0.00格式格式化为 金额单位:分 数字
	 * 
	 * @param amount 
	 * @return
	 * xxx
	 */
	public static long fmMoneyCent2Yuan(String strAmount) throws Exception {
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(2); 
		return new BigDecimal(nf.parse(strAmount).floatValue() * 100 ).longValue();
	}
	/**
	 * 格式化为百分比
	 * 
	 * @param dividend
	 * @param divisor
	 * @return
	 */
	public static String fmPercent(long dividend, long divisor) {
		return new BigDecimal(dividend).divide(new BigDecimal(divisor), 2,
				BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100))
				.toString()
				+ "%";
	}
	
	/**
	 * 格式化为百分比,不带小数
	 * 
	 * @param dividend
	 * @param divisor
	 * @return
	 */
	public static String fmPercent2(long dividend, long divisor) {
		return new BigDecimal(dividend).multiply(new BigDecimal(100))
			.divide(new BigDecimal(divisor), 0, BigDecimal.ROUND_HALF_UP) .toString() + "%";
	}

	/**
	 * 流量KB转为MB
	 * 
	 * @param flux
	 * @return
	 */
	public static String fmFlux(long flux) {
		return new BigDecimal(flux).divide(new BigDecimal(1024), 2,
				BigDecimal.ROUND_HALF_UP).toString();
	}
	
	/**
	 * 
	 * @Function: fmFluxToMBKB
	 * @Description: 流量KB转为xxMBxxKB
	 *
	 * @param flux
	 * @return
	 *
	 * @version: v1.0.0
	 * @date: 2013 Jan 3, 2013 10:11:20 PM 
	 *
	 * Modification History:
	 * Date         Author          Version            Description
	 *---------------------------------------------------------*
	 */
	public static String fmFluxKBToMBKB(long flux) {
		String result = "";
		if (flux / 1024 > 0) {
			result = flux / 1024 + "MB";
			if(0 < flux % 1024) {
				result += flux % 1024 + "KB";
			}
		} else {
			result = flux + "KB";
		}
		
		return result;
	}
	
	/**
	 *
	 * @Description: 流量KB转为xxGBxxMBxxKB
	 *
	 * @param flux
	 * @return
	 * @return String
	 *
	 * @version:v1.0.0
	 * @date:2014-11-10 下午2:50:16
	 *
	 * Modification History:
	 * Date Author Version Description
	 * -----------------------------------------------------------------
	 * 2014-11-10 liuyj5 v1.0.0 new
	 */
	public static String fmFluxKBToGBMBKB(long flux) {
		String result = "";
		if (flux == 0) {
			result = "0KB";
		} else {
			if (flux / 1024 / 1024 > 0) {
				result = flux / 1024 / 1024 + "GB";
			}
			if ((flux / 1024) % 1024 > 0) {
				result = result + (flux / 1024) % 1024 + "MB";
			}
			if (flux % 1024 > 0) {
				result = result + flux % 1024 + "KB";
			}
		}
		return result;
	}
	
	public static long fmFluxMBKBToKB(String flux) {
		long result = 0;
		int index = flux.indexOf("M");
		if (index > 0) {
			result = Long.parseLong(flux.substring(0,index)) * 1024;
			index += 2;
		} else {
			index = 0;
		}
		if(-1 < flux.indexOf("KB")) {
			result += Long.parseLong(flux.substring(index,flux.length() - 2));
		}
		
		return result;
	}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值