public class UnitConvertUtils {
/**
* 米和千米的进率
*/
public static final double RATE_OF_METRE_AND_KILOMETRE = 1000d;
public static final int INT_RATE_OF_METRE_AND_KILOMETRE = 1000;
/**
* 分和元的进率
*/
public static final double RATE_OF_FEN_AND_YUAN = 100d;
/**
* 立方厘米和立方米的进率
*/
public static final double INT_RATE_OF_CM3_AND_M3 = 1000000d;
/**
* 米转千米
*
* @param toConvert
* @return 异常返回null
*/
public static Double convertMetre2Kilometre(Long toConvert) {
if (toConvert == null) {
return null;
}
return toConvert / RATE_OF_METRE_AND_KILOMETRE;
}
/**
* 千米转米
*
* @param toConvert
* @return 异常返回null
*/
public static Long convertKilometre2Metre(Double toConvert) {
if (toConvert == null) {
return null;
}
BigDecimal bigDecimal = BigDecimal.valueOf(toConvert);
BigDecimal factorBigDecimal = BigDecimal.valueOf (RATE_OF_METRE_AND_KILOMETRE);
return bigDecimal.multiply(factorBigDecimal).longValue();
}
/**
* 元转分
*
* @param toConvert
* @return 异常返回null
*/
public static Long convertYuan2Fen(Double toConvert) {
if (toConvert == null) {
return null;
}
BigDecimal bigDecimal = BigDecimal.valueOf(toConvert);
BigDecimal factorBigDecimal = BigDecimal . valueOf(RATE_OF_FEN_AND_YUAN);
return bigDecimal.multiply(factorBigDecimal).longValue();
}
/**
* 元转分
*
* @param toConvert
* @return 异常返回null
*/
public static Long convertYuan2Fen(String toConvert) {
if (toConvert == null) {
return null;
}
BigDecimal bigDecimal = BigDecimal.valueOf (ConvertUtils.convertString 2Double(toConvert));
BigDecimal factorBigDecimal = BigDecimal. valueOf(RATE_OF_FEN_AND_YUAN);
return bigDecimal.multiply(factorBigDecimal).longValue();
}
/**
* 分转元
*
* @param price
* @return
*/
public static String convertFen2Yuan(Long price) {
if (price == null) {
return null;
}
return BigDecimal.valueOf(price).divide(new BigDecimal(RATE_OF_FEN_AND_YUAN)).toString();
}
/**
* 里程米转换为千米
*
* @param distance
* @return
*/
public static Double meter2Kilometer(Long distance) {
if (distance == null) {
return null;
}
BigDecimal meter = BigDecimal.valueOf(distance);
BigDecimal kilometer = meter.divide(new BigDecimal
(INT_RATE_OF_METRE_AND_KILOMETRE));
return kilometer.doubleValue();
}
/**
* 立方厘米转立方米
*
* @param volume
* @return
*/
public static String convertCm32M3(Long volume) {
if (volume == null) {
return null;
}
return BigDecimal.valueOf(volume).divide(new BigDecimal(INT_RATE_OF_CM3_AND_M3)).toString();
}
} |