首页

关于定义金额工具类MoneyUtil对数字金额字符串双精度型间转换、四舍五入及保留两位小数等常用处理

标签:金额工具类,MoneyUtil,双精度型转换,四舍五入,保留两位小数,BigDecimal     发布时间:2018-10-01   

一、前言

通过定义MoneyUtil金额工具类,对金额数字字符串进行转换为"BigDecimal"、保留两位小数、四舍五入、排除科学计数法的影响等常用处理,详情代码示例说明部分。

二、代码示例

import java.lang.reflect.Field;@b@import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Method;@b@import java.math.BigDecimal; @b@ @b@public class MoneyUtil {@b@@b@    private static final String HINT   = "暂无数据";@b@    private static final String PROMPT = "即将支持";@b@@b@    /**@b@     * 先将传入的字符串转换为"BigDecimal",保留两位小数后再转回String,然后在数字型字符串千分位加逗号,如果没有显示“暂无数据”@b@     * @b@     * @param str@b@     * @return@b@     */@b@    public static String moneyDataDispose(String str) {@b@        if (str != null) {@b@            BigDecimal tmpBigDecimal = new BigDecimal(str);@b@            str = StrUtil.toString(tmpBigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP));@b@            boolean neg = false;@b@            if (str.startsWith("-")) { // 处理负数@b@                str = str.substring(1);@b@                neg = true;@b@            }@b@            String tail = null;@b@            if (str.indexOf('.') != -1) { // 处理小数点@b@                tail = str.substring(str.indexOf('.'));@b@                str = str.substring(0, str.indexOf('.'));@b@            }@b@            StringBuilder sb = new StringBuilder(str);@b@            sb.reverse();@b@            for (int i = 3; i < sb.length(); i += 4) {@b@                sb.insert(i, ',');@b@            }@b@            sb.reverse();@b@            if (neg) {@b@                sb.insert(0, '-');@b@            }@b@            if (tail != null) {@b@                sb.append(tail);@b@            }@b@            return StrUtil.toString(sb) + "元";@b@        } else {@b@            return HINT;@b@        }@b@    }@b@@b@    /**@b@     * 先将传入的字符串转换为"BigDecimal",保留两位小数后再转回String,然后在数字型字符串千分位加逗号,如果没有显示“即将支持”@b@     * @b@     * @param str@b@     * @return@b@     */@b@    public static String notMoneyDataDispose(String str) {@b@        if (str != null) {@b@            BigDecimal tmpBigDecimal = new BigDecimal(str);@b@            str = StrUtil.toString(tmpBigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP));@b@            boolean neg = false;@b@            if (str.startsWith("-")) { // 处理负数@b@                str = str.substring(1);@b@                neg = true;@b@            }@b@            String tail = null;@b@            if (str.indexOf('.') != -1) { // 处理小数点@b@                tail = str.substring(str.indexOf('.'));@b@                str = str.substring(0, str.indexOf('.'));@b@            }@b@            StringBuilder sb = new StringBuilder(str);@b@            sb.reverse();@b@            for (int i = 3; i < sb.length(); i += 4) {@b@                sb.insert(i, ',');@b@            }@b@            sb.reverse();@b@            if (neg) {@b@                sb.insert(0, '-');@b@            }@b@            if (tail != null) {@b@                sb.append(tail);@b@            }@b@            return StrUtil.toString(sb) + "元";@b@        } else {@b@            return PROMPT;@b@        }@b@    }@b@@b@    /**@b@     * 四舍五入处理,不加“元”处理@b@     * @b@     * @param str@b@     * @return@b@     */@b@    public static String moneyDisposeNotAddYuan(String str) {@b@        if (StrUtil.isNotEmpty(str)) {@b@            BigDecimal tmp = new BigDecimal(str).setScale(2, BigDecimal.ROUND_HALF_UP);@b@            String tmpStr = StrUtil.toString(tmp);@b@            return tmpStr;@b@        } else {@b@            return "0";@b@        }@b@    }@b@@b@    /**@b@     * 四舍五入,如果传入参数为null,返回“暂无数据”@b@     * @b@     * @param str@b@     * @return@b@     */@b@    public static String moneyDisposeHint(String str) {@b@        if (StrUtil.isNotEmpty(str)) {@b@            BigDecimal tmp = new BigDecimal(str).setScale(2, BigDecimal.ROUND_HALF_UP);@b@            String tmpStr = StrUtil.toString(tmp);@b@            return tmpStr;@b@        } else {@b@            return HINT;@b@        }@b@    }@b@@b@    /**@b@     * 如果传入参数为字符串不为空,则返回字符串,否则返回“暂无数据”@b@     * @b@     * @param str@b@     * @return@b@     */@b@    public static String moneyDisposeHintNotAfterTwo(String str) {@b@        if (StrUtil.isNotEmpty(str)) {@b@            return str;@b@        } else {@b@            return HINT;@b@        }@b@    }@b@@b@    /**@b@     * 金额处理(排除科学计数法的影响,以及保留两位小数)@b@     * @b@     * @param bigDecimal@b@     * @return@b@     */@b@    public static String toPlainString(BigDecimal bigDecimal) {@b@        if (bigDecimal == null) {@b@            return "0.00";@b@        }@b@        return moneyDisposeNotAddYuan(bigDecimal.toPlainString());@b@    }@b@@b@    /**@b@     * 金额四舍五入,保留两位小数(兼容处理科学计数法数据),如果传入参数为null,则返回null,如果传入参数为0,则返回0.00@b@     * @param money@b@     * @return@b@     */@b@    public static BigDecimal keepTwoDecimalPlaces(BigDecimal money) {@b@        if (money != null) {@b@            if (money.compareTo(new BigDecimal(0)) == 0) {@b@                return new BigDecimal("0.00");@b@            } else {@b@                String toPlan = money.toPlainString();@b@                return new BigDecimal(toPlan).setScale(2, BigDecimal.ROUND_HALF_UP);@b@            }@b@        } else {@b@            return money;@b@        }@b@    }@b@@b@    /**@b@     * 为实体有BigDecimal类型的属性进行处理,暂不支持属性中嵌套实体的BigDecimal类型属性值处理@b@     * @param model@b@     * @throws NoSuchMethodException@b@     * @throws IllegalAccessException@b@     * @throws IllegalArgumentException@b@     * @throws InvocationTargetException@b@     * @throws SecurityException@b@     * @throws InstantiationException@b@     */@b@    public static void putBigDecimalReflect(Object model) throws NoSuchMethodException,@b@                                                         IllegalAccessException,@b@                                                         IllegalArgumentException,@b@                                                         InvocationTargetException,@b@                                                         SecurityException, InstantiationException {@b@        if (model != null) {@b@            Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组  @b@            for (int j = 0; j < field.length; j++) { //遍历所有属性@b@                String name = field[j].getName(); //获取属性的名字@b@                name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());@b@                String type = field[j].getGenericType().toString(); //获取属性的类型@b@                if (type.equals("class java.math.BigDecimal")) { //如果type是类类型,则前面包含"class ",后面跟类名@b@                    Method m = model.getClass().getMethod("get" + name);@b@                    BigDecimal value = (BigDecimal) m.invoke(model); //调用getter方法获取属性值@b@                    //关键。。。可访问私有变量@b@                    field[j].setAccessible(true);@b@                    field[j].set(model, keepTwoDecimalPlaces(value));@b@                }@b@            }@b@        }@b@    }@b@}