CompareTo的比较原理:
先读取出字符串的第一个“字母”进行比较,比较的方法是ascii码表的值(字符所对应的十进制值),如果前面的大那么返回1,后面的大返回-1;此位置相同,继续比较下一位,直到最后一位,如果都相同的话,就返回0;
例如:
int cc= "13".compareTo("12");
System.out.println(cc);
int dd= "11".compareTo("12");
System.out.println(cc);
int dd= "aaa".compareTo("aaa");
System.out.println(cc);
结果就是:1(换行) -1(换行)0。
运用:
在做资金拆分的时候要校验拆分, 就是通比较两个 输入的金额和主表的余额(需要拆分的金额), 通过返回结果(0或者-1)到前台页面,当然这个也是通过ajax请求来做的。
// 拆分金额校验
@ResponseBody
@RequestMapping("/checkAmt")
public int checkAmt(Double amt, Long detailId, Long fundsDirection) {
ReconFundsDetailForQuery query = new ReconFundsDetailForQuery();
query.setId(detailId);
List<ReconFundsDetailDto> dto_l = reconFundsDetailService.query(query);
ReconFundsDetailDto dto = dto_l.get(0);
Long fundsAmtIn = dto.getFundsAmtIn() != null ? dto.getFundsAmtIn() : LONG_ZERO;// 入金
Long fundsAmtOut = dto.getFundsAmtOut() != null ? dto.getFundsAmtOut() : LONG_ZERO;// 出金
BigDecimal bigAmt = new BigDecimal(String.valueOf(amt));// 拆分金额
Map<String, Object> paramMap = new HashMap<String, Object>();
List<Long> aprrovalStatus = new ArrayList<Long>();
aprrovalStatus.add(AprrovalStatus.COMMITED.getKey());
aprrovalStatus.add(AprrovalStatus.SYNCHRONIZED.getKey());
paramMap.put("aprrovalStatus", aprrovalStatus);
paramMap.put("parentFundsId", detailId);
Long commitedAmt = LONG_ZERO;
Long CommitedTotalAmt = reconFundsDetailTmpService.queryCommitedTotalAmt(paramMap); // 大于0入金多,小于0出金多
if (CommitedTotalAmt != null) {
commitedAmt = CommitedTotalAmt;
}
if (fundsAmtIn.longValue() != LONG_ZERO && fundsAmtOut.longValue() == LONG_ZERO) {// 明细表入金类型
if (fundsDirection == FundDirection.IN.getKey()) {// 用户选择是入金
BigDecimal balance = NumberUtils.longToBigDecimal(fundsAmtIn - commitedAmt);
if (balance.compareTo(bigAmt) == -1) {
return -1;
}
}
}
if (fundsAmtIn.longValue() == LONG_ZERO && fundsAmtOut.longValue() != LONG_ZERO) {// 明细表出金类型
if (fundsDirection == FundDirection.OUT.getKey()) {// 用户选择是出金
BigDecimal balance = NumberUtils.longToBigDecimal(fundsAmtOut + commitedAmt);
if (balance.compareTo(bigAmt) == -1) {
return -1;
}
}
}
if (fundsAmtIn.longValue() == LONG_ZERO && fundsAmtOut.longValue() == LONG_ZERO) {
return -1;
}
return 0;
}