
commons.box.util.Nums Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-box-app Show documentation
Show all versions of commons-box-app Show documentation
Common utils for BOX projects.
The newest version!
package commons.box.util;
import javax.annotation.Nonnull;
import java.math.BigDecimal;
import java.util.function.Function;
import static java.math.BigDecimal.ZERO;
import static java.math.RoundingMode.HALF_UP;
/**
* 与数值相关的操作
*/
public final class Nums {
public static final BigDecimal MIN = bd(0.00000001);
/** 获取 BigDecimal */
@Nonnull
public static BigDecimal bd(BigDecimal bd) {
if (bd == null) return ZERO;
return bd.setScale(8, HALF_UP);
}
/** 获取 BigDecimal 含精度 */
@Nonnull
public static BigDecimal bd(BigDecimal bd, int scale) {
if (bd == null) return ZERO;
return bd.setScale(scale, HALF_UP);
}
/** 获取 BigDecimal */
@Nonnull
public static BigDecimal bd(double num) {
return BigDecimal.valueOf(num).setScale(8, HALF_UP);
}
/** 获取 BigDecimal */
@Nonnull
public static BigDecimal bd(long num) {
return BigDecimal.valueOf(num).setScale(8, HALF_UP);
}
/** 判断是否为 0 */
public static boolean is0(Number number) {
if (number == null) return true;
else if (number instanceof BigDecimal) return ZERO.equals(number);
else if (number instanceof Long) return number.longValue() == 0;
else if (number instanceof Integer) return number.intValue() == 0;
else return Math.abs(number.doubleValue()) < 0.00000001;
}
/** 判断是否大于等于 0 */
public static boolean ge0(Number number) {
if (number == null) return false;
else if (number instanceof BigDecimal) return bd((BigDecimal) number).compareTo(ZERO) >= 0;
else if (number instanceof Long) return number.longValue() >= 0;
else if (number instanceof Integer) return number.intValue() >= 0;
else return bd(number.doubleValue()).setScale(18, HALF_UP).compareTo(ZERO) >= 0;
}
/** 判断是否大于 0 */
public static boolean gt0(Number number) {
if (number == null) return false;
else if (number instanceof BigDecimal) return bd((BigDecimal) number).compareTo(MIN) > 0;
else if (number instanceof Long) return number.longValue() > 0;
else if (number instanceof Integer) return number.intValue() > 0;
else return bd(number.doubleValue()).setScale(18, HALF_UP).compareTo(MIN) > 0;
}
/** 控制精度的运算 */
public static BigDecimal scale2(BigDecimal n, Function func) {
return scale(n, 2, func);
}
/** 控制精度的运算 */
public static BigDecimal scale4(BigDecimal n, Function func) {
return scale(n, 4, func);
}
/** 控制精度的运算 */
public static BigDecimal scale(BigDecimal n, int scale, Function func) {
return scale(n, scale, 32, func);
}
/** 控制精度的运算 */
public static BigDecimal scale(BigDecimal n, int scale, int innerScale, Function func) {
n = bd(n, innerScale);
if (func != null) return bd(func.apply(n)).setScale(scale, HALF_UP);
else return n.setScale(scale, HALF_UP);
}
public static void main(String[] args) {
Number[] o = new Number[]{
bd(0), bd(1), bd(0.00001),
0.0, 0.00001, 0.0000001, 0.0000000001, -0.00001
};
for (Number o1 : o) {
System.out.println("数据 is0=" + o1 + " type=" + o1.getClass() + "\t" + is0(o1) + " ge0=" + ge0(o1) + " gt0=" + gt0(o1));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy