技术交流群:233513714
/**
* 金额校验,判断两个金额是否相等
*
* @param money_1 金额单位为元(网关返回金额)
* @param money_2 金额单位为分(用户下单金额)
* @return
*/
public static boolean CheckAmount(String money_1, Integer money_2) {
try {
BigDecimal m_1 = new BigDecimal(changeY2F(money_1));
BigDecimal m_2 = BigDecimal.valueOf(money_2);
log.info("比对金额m_1:" + m_1 + "----m_2:" + m_2);
if (m_1.compareTo(m_2) == 0) {
return true;
} else {
return false;
}
} catch (Exception e) {
log.info("金额判断出现错误:money_1-->" + money_1 + "----money_2-->" + money_2);
log.info("错误信息:" + e);
}
return false;
}
/**
* 将元为单位的转换为分 替换小数点,支持以逗号区分的金额
*
* @param amount
* @return
*/
public static String changeY2F(String amount){
String currency = amount.replaceAll("\$|\¥|\,", ""); //处理包含, ¥ 或者$的金额
int index = currency.indexOf(".");
int length = currency.length();
Long amLong = 0l;
if(index == -1){
amLong = Long.valueOf(currency+"00");
}else if(length - index >= 3){
amLong = Long.valueOf((currency.substring(0, index+3)).replace(".", ""));
}else if(length - index == 2){
amLong = Long.valueOf((currency.substring(0, index+2)).replace(".", "")+0);
}else{
amLong = Long.valueOf((currency.substring(0, index+1)).replace(".", "")+"00");
}
return amLong.toString();
}