数字工具类

本文深入探讨了数字工具类的功能,包括判断整数与数字、格式化输出、数值转换等核心操作,提供了丰富的示例与解析,帮助开发者高效处理数字相关任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
    }

	
}

/** * @project: WebProjectUtil * @class: NumberUtil * @describe: 此工具类用来处理数字方面的逻辑, * 如返回指定位数的随机数字、Double的加减乘除精确运算、指定位数数字用“0”补齐 * @autho: Administrator * @date: 2013-6-7 下午02:26:27 * @alter: Administrator * @alterDate: 2013-6-7 下午02:26:27 * @alterRemark: * @version V1.0 */ public class NumberUtil { private static final int DEF_DIV_SCALE = 2; /** * @return 返回12位随机数 */ public static String randomNumber() { } /** * @param parm * @return 返回指定位数随机数 */ public static String randomNumber(int parm) { } /** * * 两个Double数相加 * * @param v1 * @param v2 * @return Double */ public static Double add(Double v1, Double v2) { } /** * * 两个Double数相减 * * @param v1 * @param v2 * @return Double */ public static Double sub(Double v1, Double v2) { } /** * * 两个Double数相乘 * * @param v1 * @param v2 * @return Double */ public static Double mul(Double v1, Double v2) { } /** * * 两个Double数相除 * * @param v1 * @param v2 * @return Double */ public static Double div(Double v1, Double v2) { } /** * * 两个Double数相除,并保留scale位小数 * * @param v1 * @param v2 * @param scale * @return Double */ public static Double div(Double v1, Double v2, int scale) { } /** * 返回指定Double的负数 * @param v1 * @return */ public static Double neg(Double v1) { /** * @Title: toFixdLengthString * @Description: 将字符串用符号填充位数 * @param str 源字符串 * @param fixdlenth 位数 * @return String * @throws */ public static String toFixdLengthString(String str, int fixdlenth) { } /** * @Title: toFixdLengthString * @Description: 将数字用“0”填充位数 * @param num * @param fixdlenth * @return String * @throws */ public static String toFixdLengthString(int num, int fixdlenth) { } /** * @Title: generateSpaceString * @Description: 得到指定位数占位符 * @param length * @return String * @throws */ public static String generateSpaceString(int length) { } /** * @Title: generateZeroString * @Description: 得到指定位数的“0”的占位符 * @param length * @return String * @throws */ public static String generateZeroString(int length) { } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值