数字格式转换相关工具类

1.获取千分位格式,保留两位小数,不足补0

	/**
     * 获取千分位格式,并且整数补充0
     *
     * @return 字符串
     * @throws IllegalArgumentException 如果传入的参数不是数字类型
     */
    public static String getThousands(Object number) {
        try {
            if (ObjectUtils.isEmpty(number)) {
                return null;
            }
            if (number instanceof Number) {
                BigDecimal bigDecimalObj = new BigDecimal(number.toString()).setScale(2, RoundingMode.HALF_UP); // 设置保留两位小数并四舍五入
                DecimalFormat decimalFormat = new DecimalFormat("#,###.##");
                String bigDecimalFormat = decimalFormat.format(bigDecimalObj);

                boolean containsDot = bigDecimalFormat.contains(".");

                if (!containsDot) {
                    bigDecimalFormat = bigDecimalFormat.concat(".");
                }

                String[] parts = bigDecimalFormat.split("\\.");

                if (parts.length > 1) {
                    int decimalPlaces = parts[1].length();

                    int needDecimalPlaces = 2; // 需要补充的位数
                    if (decimalPlaces < needDecimalPlaces) {
                        int zerosToAdd = needDecimalPlaces - decimalPlaces;
                        for (int i = 0; i < zerosToAdd; i++) {
                            bigDecimalFormat = bigDecimalFormat.concat("0");
                        }
                    }
                } else {
                    bigDecimalFormat = bigDecimalFormat.concat("00");
                }
                return bigDecimalFormat;
            }
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("");
        }
        return null;
    }

2.数字保留两位小数,不足补0

/**
     * 数字保留两位小数,不足补0
     * @param number
     * @return
     */
    public static String getRepairTwoZero(Object number) {
        try {
            if (ObjectUtils.isEmpty(number)) {
                return null;
            }
            if (number instanceof Number) {
                BigDecimal bigDecimalObj = new BigDecimal(number.toString()).setScale(2, RoundingMode.HALF_UP); // 设置保留两位小数并四舍五入
                DecimalFormat decimalFormat = new DecimalFormat("##.##");
                String bigDecimalFormat = decimalFormat.format(bigDecimalObj);

                boolean containsDot = bigDecimalFormat.contains(".");

                if (!containsDot) {
                    bigDecimalFormat = bigDecimalFormat.concat(".");
                }

                String[] parts = bigDecimalFormat.split("\\.");

                if (parts.length > 1) {
                    int decimalPlaces = parts[1].length();

                    int needDecimalPlaces = 2; // 需要补充的位数
                    if (decimalPlaces < needDecimalPlaces) {
                        int zerosToAdd = needDecimalPlaces - decimalPlaces;
                        for (int i = 0; i < zerosToAdd; i++) {
                            bigDecimalFormat = bigDecimalFormat.concat("0");
                        }
                    }
                } else {
                    bigDecimalFormat = bigDecimalFormat.concat("00");
                }
                return bigDecimalFormat;
            }
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("");
        }
        return null;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值