NumberUtil

package com.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;


public final class NumberUtil {
    private static final DecimalFormat myformat = new DecimalFormat("0.00");


    /**
     * 如果是null 则返回0
     *
     * @param value
     * @return
     */
    public static boolean isNull(Number value) {
    	return value==null;
    }
    
    
    /**
     * 将数字格式化成2位小数
     *
     * @param number
     * @return
     */
    public static String fmt2p(Number number) {
        return myformat.format(number);
    }

    /**
     * 判断数值o1 和数值o2是否相等
     *
     * @param o1
     * @param o2
     * @return
     */
    public static boolean isEquals(Number o1, Number o2) {
        if (o1 == o2) {
            return true;
        } else if (o1 == null || o2 == null) {
            return false;
        } else if (o1 instanceof Double || o1 instanceof Float || o2 instanceof Double || o2 instanceof Float) {
            return o1.doubleValue() == o2.doubleValue();
        } else {
            return o1.longValue() == o2.longValue();
        }
    }

    public static Short parseShort(Object o) {
        if (o instanceof Number) {
            return ((Number) o).shortValue();
        } else if (o instanceof String) {
            try {
                return Short.parseShort((String) o);
            } catch (NumberFormatException e) {
                Double d = parseDouble(o);
                if (d != null)
                    return d.shortValue();
            }
        }
        return null;
    }

    public static Byte parseByte(Object o) {
        if (o instanceof Number) {
            return ((Number) o).byteValue();
        } else if (o instanceof String) {
            try {
                return Byte.parseByte((String) o);
            } catch (NumberFormatException e) {
                Double d = parseDouble(o);
                if (d != null)
                    return d.byteValue();
            }
        }
        return null;
    }


    public static Integer parseInt(Object o) {
        if (o instanceof Number) {
            return ((Number) o).intValue();
        } else if (o instanceof String) {
            try {
                return Integer.parseInt((String) o);
            } catch (NumberFormatException e) {
                Double d = parseDouble(o);
                if (d != null)
                    return d.intValue();
            }
        }
        return null;
    }

    public static Long parseLong(Object o) {
        if (o instanceof Number) {
            return ((Number) o).longValue();
        } else if (o instanceof String) {
            try {
                return Long.parseLong((String) o);
            } catch (NumberFormatException e) {
                Double d = parseDouble(o);
                if (d != null)
                    return d.longValue();
            }
        }
        return null;
    }

    /**
     * 如果是null 则返回0
     *
     * @param value
     * @return
     */
    public static Number isNull0(Number value) {
        return isNull(value, 0);
    }

    /**
     * 如果是null 则返回newValue
     *
     * @param value
     * @return
     */
    public static Number isNull(Number value, Number newValue) {
        return value == null ? 0 : newValue;
    }

    /**
     * 如果是null 则返回newValue
     *
     * @param value
     * @return
     */
    public static Number isNull0D(Number value, Number newValue) {
        return value == null ? 0D : newValue;
    }

    /**
     * 如果是null 则返回newValue
     *
     * @param value
     * @return
     */
    public static Number isNull0L(Number value, Number newValue) {
        return value == null ? 0L : newValue;
    }
    /**
     * 如果是null
     *
     * @param value
     * @return
     */
    public static boolean  isNull(Integer value) {
    	return value == null;
    }

    public static Float parseFloat(Object o) {
        if (o instanceof Number) {
            return ((Number) o).floatValue();
        } else if (o instanceof String) {
            try {
                return Float.parseFloat((String) o);
            } catch (NumberFormatException e) {
                Double d = parseDouble(o);
                if (d != null)
                    return d.floatValue();
            }
        }
        return null;
    }

    public static Double parseDouble(Object o) {
        if (o instanceof Number) {
            return ((Number) o).doubleValue();
        } else if (o instanceof String) {
            try {
                return Double.parseDouble((String) o);
            } catch (NumberFormatException e) {
            }
        }
        return null;
    }

    /**
     * Description: 四舍五入
     * @param enDouble 被四舍五入值
     * @param rountNum 精确到的位数:如:0,保留整数;1,保留一位小数
     * @return
     */
    public static Double roundDouble(Double enDouble,int rountNum){
    	if (enDouble == null) {
			return null;
		}
    	
    	BigDecimal bd = new BigDecimal(enDouble);
    	return bd.setScale(rountNum, BigDecimal.ROUND_HALF_UP).doubleValue();
    	
    }


    /**
     * 取整舍去
     * @param enDouble
     * @param rountNum
     * @return
     */
    public static Double roundDoubleDown(Double enDouble,int rountNum){
        if (enDouble == null) {
            return null;
        }

        BigDecimal bd = new BigDecimal(enDouble);
        return bd.setScale(rountNum, BigDecimal.ROUND_DOWN).doubleValue();

    }
    
    private NumberUtil() {

    }
    
	
	/**
     * Description:金额四舍五入两位小数
     * @param num
     * @return
     */
    public static double get2Double(double num) {
        DecimalFormat df = new DecimalFormat("0.00");
        return new Double(df.format(num).toString());
    }
}



在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值