Java 金额计算工具类 元转分 分转元

public class CalculationUtil {

    /**
     * 元转分进率
     */
    public static final BigDecimal YUAN_TRANSFER_RATE = new BigDecimal(100);

    /**
     * 类型转换
     * 任意类型转为 BigDecimal
     *
     * @param text
     * @return
     */
    private static BigDecimal typeConversion(@NonNull Object text) throws Exception {
        BigDecimal text_1 = null;
        if (text instanceof Integer) {
            int value = ((Integer) text).intValue();
            text_1 = new BigDecimal(value);
        } else if (text instanceof String) {
            String s = (String) text;
            if (isInteger(s) || isDouble(s)) {
                text_1 = new BigDecimal(s);
            } else {
                throw new Exception("传入数值不对,需传入数字或小数");
            }
        } else if (text instanceof Double) {
            double d = ((Double) text).doubleValue();
            text_1 = new BigDecimal(d);
        } else if (text instanceof Float) {
            float f = ((Float) text).floatValue();
            text_1 = new BigDecimal(f);
        } else if (text instanceof Long) {
            long l = ((Long) text).longValue();
            text_1 = new BigDecimal(l);
        } else if (text instanceof Boolean) {
            boolean b = ((Boolean) text).booleanValue();
            throw new Exception("传入数值不对,需传入数字或小数");
        } else if (text instanceof Date) {
            Date d = (Date) text;
            throw new Exception("传入数值不对,需传入数字或小数");
        } else {
            text_1 = (BigDecimal) text;
        }
        return text_1;
    }

    //判断整数(int)
    public static boolean isInteger(String str) {
        if (null == str || "".equals(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    //判断浮点数(double和float)
    public static boolean isDouble(String str) {
        if (null == str || "".equals(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("[+-]?[0-9]+\\.([0-9]+)"); // 之前这里正则表达式错误,现更正
        return pattern.matcher(str).matches();
    }

    /**
     * 使用java正则表达式去掉多余的.与0
     *
     * @param s
     * @return string
     */
    public static BigDecimal replace(@NonNull String s) {
        if (null != s && s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return new BigDecimal(s);
    }

    /**
     * 元转分
     *
     * @param text
     * @return
     */
    public static BigDecimal yuanToPoints(@NonNull Object text) {
        BigDecimal yuan = null;
        try {
            yuan = typeConversion(text);
        } catch (Exception e) {
           e.printStackTrace();
            return yuan;
        }
        //元转分计算 除以以进率
        return replace(rounding(yuan).multiply(YUAN_TRANSFER_RATE).toString());
    }

    /**
     * 分转元
     *
     * @param text
     * @return
     */
    public static BigDecimal centsToYuan(@NonNull Object text) {
        BigDecimal fen = null;
        try {
            fen = typeConversion(text);
        } catch (Exception e) {
           e.printStackTrace();
            return fen;
        }
        //分转元计算 乘以进率
        return replace(fen.divide(YUAN_TRANSFER_RATE).toString());
    }

    /**
     * BigDecimal四舍五入保留两位小数
     *
     * @param bigDecimal
     * @return
     */
    public static BigDecimal rounding(@NonNull BigDecimal bigDecimal) {
        return bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 判断字符串是否为数字(正整数和浮点数)
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        String reg = "^[0-9]+(.[0-9]+)?$";
        Pattern pattern = Pattern.compile(reg);
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

//    public static void main(String[] args) {
//        String a = "111.238";
//        BigDecimal bigDecimal = new BigDecimal(20000);
//        BigDecimal b = yuanToPoints(bigDecimal);
//        System.out.println(b.toString());
//        System.out.println(rounding(b));
//        System.out.println(replace(rounding(b).toString()));
//    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一名技术极客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值