数字工具类
来源:互联网 发布:淘宝手机商品管理软件 编辑:程序博客网 时间:2024/06/06 07:47
package com.yunmall.framework.core.util;import java.math.BigDecimal;import java.math.BigInteger;import java.util.regex.Pattern;/** * 数字工具类 * * @version 1.0 * @createDate 2012-7-31 * @modifyDate 2012-8-14 */public class NumberUtils { /** * 判断当前值是否为整数 * * @param value * 要判断的对象 * @return 是否为整数 */ public static boolean isInteger(Object value) { if (StringUtil.isEmpty(value)) { return false; } String mstr = value.toString(); Pattern pattern = Pattern.compile("^-?\\d+{1}"); return pattern.matcher(mstr).matches(); } /** * 判断当前值是否为数字(包括小数) * * @param value * 要判断的对象 * @return 是否为数字 */ public static boolean isDigit(Object value) { if (StringUtil.isEmpty(value)) { return false; }String mstr = value.toString();// modify by liximeng Pattern pattern = Pattern.compile("^-?[0-9]*\\.?[0-9]*{1}");return pattern.matcher(mstr).matches();} /** * 将数字格式化输出 * * @param value * 需要格式化的值 * @param precision * 精度(小数点后的位数) * @return 格式化后的结果 */ public static String format(Object value, Integer precision) { Double number = 0.0; if (NumberUtils.isDigit(value)) { number = new Double(value.toString()); } precision = (precision == null || precision < 0) ? 2 : precision; BigDecimal bigDecimal = new BigDecimal(number); return bigDecimal.setScale(precision, BigDecimal.ROUND_HALF_UP).toString(); } /** * 将数字格式化输出,取2位小数 * * @param value * 数字 * @return 格式化后的结果 */ public static String format(Object value) { return NumberUtils.format(value, 2); } /** * 将值转成Integer型, 如果为0或者null,则返回replace的值 * * @param value * 原值 * @param replace * 替换值 * @return 转换结果 */ public static Integer parseInteger(Object value, Integer replace) { if (!NumberUtils.isInteger(value)) { return replace; } return new Integer(value.toString()); } /** * 将值转成Integer型,如果不是整数,则返回0 * * @param value * 原值 * @return 转换结果 */ public static Integer parseInteger(Object value) { return NumberUtils.parseInteger(value, 0); } /** * 将值转成Long型,如果为0或者null,则返回replace的值 * * @param value * 原值 * @param replace * 替换值 * @return 转换结果 */ public static Long parseLong(Object value, Long replace) { if (!NumberUtils.isInteger(value)) { return replace; } return new Long(value.toString()); } /** * 将值转成Long型,如果不是整数,则返回0 * * @param value * 原值 * @return 转换结果 */ public static Long parseLong(Object value) { return NumberUtils.parseLong(value, 0L); } /** * 将值转成Double型,如果为0或者null,则返回replace的值 * * @param value * 原值 * @param replace * 替换值 * @return 转换结果 */ public static Double parseDouble(Object value, Double replace) { if (!NumberUtils.isDigit(value)) { return replace; } return new Double(value.toString()); } /** * 将值转成Double型,如果不是整数,则返回0 * * @param value * 原值 * @return 转换结果 */ public static Double parseDouble(Object value) { return NumberUtils.parseDouble(value, 0.0); } /** * 将char型数据转成字节数组 * * @param value * 原值 * @return 转换结果 */ public static byte[] toBytes(char value) { byte[] bt = new byte[2]; for (int i = 0; i < bt.length; i++) { bt[i] = (byte) (value >>> (i * 8)); } return bt; } /** * 将short型数据转成字节数组 * * @param value * 原值 * @return 转换结果 */ public static byte[] toBytes(short value) { byte[] bt = new byte[2]; for (int i = 0; i < bt.length; i++) { bt[i] = (byte) (value >>> (i * 8)); } return bt; } /** * 将int型数据转成字节数组 * * @param value * 原值 * @return 转换结果 */ public static byte[] toBytes(int value) { byte[] bt = new byte[4]; for (int i = 0; i < bt.length; i++) { bt[i] = (byte) (value >>> (i * 8)); } return bt; } /** * 将long型数据转成字节数组 * * @param value * 原值 * @return 转换结果 */ public static byte[] toBytes(long value) { byte[] bt = new byte[8]; for (int i = 0; i < bt.length; i++) { bt[i] = (byte) (value >>> (i * 8)); } return bt; } /** * 将short型数据插入到指定索引的字节数组中 * * @param index * 索引 * @param values * 字节数组 * @param value * 需要插入的值 */ public static void insert(int index, byte[] values, short value) { byte[] bt = NumberUtils.toBytes(value); System.arraycopy(bt, 0, values, index, 2); } /** * 将int型数据插入到指定索引的字节数组中 * * @param index * 索引 * @param values * 字节数组 * @param value * 需要插入的值 */ public static void insert(int index, byte[] values, int value) { byte[] bt = NumberUtils.toBytes(value); System.arraycopy(bt, 0, values, index, 4); } /** * 将long型数据插入到指定索引的字节数组中 * * @param index * 索引 * @param values * 字节数组 * @param value * 需要插入的值 */ public static void insert(int index, byte[] values, long value) { byte[] bt = NumberUtils.toBytes(value); System.arraycopy(bt, 0, values, index, 8); } /** * 将字节转换成整型 * * @param value * 字节类型值 * @return 转换结果 */ public static int byteToInt(byte value) { return value < 0 ? value + 256 : value;}/** * 转换 BigDecimal 为不带指数格式的字符串,默认返回"0" * * @param d * @return */public static String bigDecimalToString(BigDecimal d) {if (d != null) {return d.toPlainString();}return "0";}/** * 转换 为 BigDecimal类型 * * @param value * @return */public static BigDecimal strToBigDecimal(Object value) {if (!isDigit(value)) {value = "0";}return new BigDecimal(value.toString());}/** * 将值转成Float型,如果为0或者null,则返回replace的值 * * @param value * @param replace 替换值 * @return */ public static Float parseFloat(Object value, Float replace) { if (!isDigit(value)) return replace; return new Float(value.toString()); } /** * 将值转成Float型,如果不是整数,则返回0 * * @param value * @return */ public static Float parseFloat(Object value) { return NumberUtils.parseFloat(value, 0.0f); } /** * 将值转成BigInteger型, 如果为0或者null,则返回replace的值 * * @param value * @param replace * 替换值 * @return */ public static BigInteger parseBigInteger(Object value, BigInteger replace) { if (!isInteger(value)) return replace; return new BigInteger(value.toString()); } /** * 将值转成BigInteger型,如果不是整数,则返回0 * * @param value * @return */ public static BigInteger parseBigInteger(Object value) { return NumberUtils.parseBigInteger(value, BigInteger.ZERO); } /** * 将值转成BigDecimal型, 如果为0或者null,则返回replace的值 * * @param value * @param replace * 替换值 * @return */ public static BigDecimal parseBigDecimal(Object value, BigDecimal replace) { if (!isDigit(value)) return replace; return new BigDecimal(value.toString()); } /** * 将值转成BigDecimal型,如果不是数字,则返回0 * * @param value * @return */ public static BigDecimal parseBigDecimal(Object value) { return NumberUtils.parseBigDecimal(value, BigDecimal.ZERO); }}