package com.jarvis.base.util;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;
public class NumericHelper {
/**
* 描述:通过一个整数i获取你所要的哪几个(从0开始) i为 多个2的n次方之和,如i=7,那么根据原值是2的n次方之各,你的原值必定是1,2,4 。
*
* @param i
* 数值
* @return
*/
public static int[] getWhich(long i) {
int exp = Math.getExponent(i);
if (i == (1 << (exp + 1)) - 1) {
exp = exp + 1;
}
int[] num = new int[exp];
int x = exp - 1;
for (int n = 0; (1 << n) < i + 1; n++) {
if ((1 << (n + 1)) > i && (1 << n) < (i + 1)) {
num[x] = n;
i -= 1 << n;
n = 0;
x--;
}
}
return num;
}
/**
* 描述:非四舍五入取整处理
*
* @param v
* 需要四舍五入的数字
* @return
*/
public static int roundDown(double v) {
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, 0, BigDecimal.ROUND_DOWN).intValue();
}
/**
* 描述:四舍五入取整处理
*
* @param v
* 需要四舍五入的数字
* @return
*/
public static int roundUp(double v) {
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(o
Java常用工具类---数字工具类
最新推荐文章于 2024-08-08 16:57:52 发布
本文详细介绍了Java中常用的数字工具类,包括其主要方法、功能及在实际开发中的应用示例,帮助读者深入理解如何高效处理数字操作。
摘要由CSDN通过智能技术生成