All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dennisit.vplus.data.utils.NumberUtils Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
 */
package com.github.dennisit.vplus.data.utils;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;

/**
 * Created by Elon.su on 17/5/13.
 */
@Deprecated
@Slf4j
public final class NumberUtils {

    /* 约束数值*/
    public static final int RESTRAIN_NUM_0 = 0;
    public static final int RESTRAIN_NUM_1 = 1;
    public static final int RESTRAIN_NUM_5 = 5;
    public static final int RESTRAIN_NUM_10 = 10;
    public static final int RESTRAIN_NUM_25 = 25;
    public static final int RESTRAIN_NUM_50 = 50;
    public static final int RESTRAIN_NUM_100 = 100;

    private static final int HUNDRED = 100;

    public static long valueOf(Long num) {
        return valueOf(num, 0);
    }

    public static long valueOf(Long num, long defaultVal) {
        if (null == num) {
            return defaultVal;
        }
        return num.longValue();
    }

    public static int valueOf(Integer num) {
        return valueOf(num, 0);
    }

    public static int valueOf(Integer num, int defaultVal) {
        if (null == num) {
            return defaultVal;
        }
        return num.intValue();
    }

    public static byte valueOf(Byte num) {
        return valueOf(num, (byte) 0);
    }

    public static byte valueOf(Byte num, byte defaultVal) {
        if (null == num) {
            return defaultVal;
        }
        return num.byteValue();
    }

    public static String valueOf(String str) {
        return valueOf(str, StringUtils.EMPTY);
    }

    public static double valueOf(Double num) {
        return valueOf(num, 0);
    }

    public static double valueOf(Double num, double defaultVal) {
        if (null == num) {
            return defaultVal;
        }
        return num.doubleValue();
    }

    public static float valueOf(Float num, float defaultVal) {
        if (null == num) {
            return defaultVal;
        }
        return num.floatValue();
    }

    public static String valueOf(String str, String defaultVal) {
        if (StringUtils.isBlank(str)) {
            return defaultVal;
        }
        return StringUtils.trim(str);
    }

    /**
     * 闭区间约束数字
     *
     * @param number 待约束的数值
     * @param min    最小值
     * @param max    最大值
     * @return 约束结果值
     */
    public static int restrainNum(int number, int min, int max) {
        return (number <= min) ? min : (number >= max ? max : number);
    }


    public static int[] charToInt(char[] ca) {
        int len = ca.length;
        int[] iArr = new int[len];
        try {
            for (int i = 0; i < len; i++) {
                iArr[i] = Integer.parseInt(String.valueOf(ca[i]));
            }
        } catch (NumberFormatException e) {
            log.error(e.getLocalizedMessage(), e);
        }
        return iArr;
    }

    /**
     * 将(十进制,八进制,十六进制) 字符串格式数字,转换为int
     *
     * @param str          目标字符串
     * @param defaultValue 默认值
     * @return 十进制结果
     */
    public static Integer decode2int(String str, Integer defaultValue) {
        if (str == null) {
            return defaultValue;
        }

        try {
            //xxx 用Long转,可以避免 0xffee0011 这种解析的逸出 Integer.decode 只能转整数,负数会报错
            return Long.decode(str).intValue();
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static String toHex(int hex) {
        String str = Integer.toHexString(hex);
        if (str.length() == 1) {
            return "0" + str;
        }

        return str;
    }


    public static double divide(double top, double bottom) {
        return divide(top, bottom, 2);
    }


    public static double divide(double top, double bottom, int decimal) {
        return new BigDecimal(top).divide(new BigDecimal(bottom), decimal, BigDecimal.ROUND_HALF_EVEN).doubleValue();
    }

    public static int percent2Int(double num) {
        return new BigDecimal(num).multiply(new BigDecimal(String.valueOf(HUNDRED))).intValue();
    }

    public static double int2Percent(int num) {
        return divide(num, HUNDRED);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy