com.github.dennisit.vplus.data.utils.NumberUtils Maven / Gradle / Ivy
/*--------------------------------------------------------------------------
* 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 java.math.BigDecimal;
/**
* Created by Elon.su on 17/5/13.
*/
public 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 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);
}
}