mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
NumberUtil.pow支持负数
This commit is contained in:
parent
d58b2fcfea
commit
503df65777
@ -2,7 +2,7 @@
|
||||
# 🚀Changelog
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.28(2024-05-23)
|
||||
# 5.8.28(2024-05-29)
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 修正XmlUtil的omitXmlDeclaration描述注释(issue#I9CPC7@Gitee)
|
||||
@ -25,6 +25,7 @@
|
||||
* 【http 】 getFileNameFromDisposition更加规范,从多个头的值中获取,且`filename*`优先级更高(pr#3590@Gitee)
|
||||
* 【core 】 CsvWriter增加重载writeBeans方法,支持可选bean字段(pr#1222@Gitee)
|
||||
* 【core 】 LocalDateTimeUtil增加beginOfDay和endOfDay重载(issue#3594@Github)
|
||||
* 【core 】 NumberUtil.pow支持负数(issue#3598@Github)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题(issue#3536@Github)
|
||||
|
@ -49,9 +49,9 @@ public class NumberUtil {
|
||||
* 0-20对应的阶乘,超过20的阶乘会超过Long.MAX_VALUE
|
||||
*/
|
||||
private static final long[] FACTORIALS = new long[]{
|
||||
1L, 1L, 2L, 6L, 24L, 120L, 720L, 5040L, 40320L, 362880L, 3628800L, 39916800L, 479001600L, 6227020800L,
|
||||
87178291200L, 1307674368000L, 20922789888000L, 355687428096000L, 6402373705728000L, 121645100408832000L,
|
||||
2432902008176640000L};
|
||||
1L, 1L, 2L, 6L, 24L, 120L, 720L, 5040L, 40320L, 362880L, 3628800L, 39916800L, 479001600L, 6227020800L,
|
||||
87178291200L, 1307674368000L, 20922789888000L, 355687428096000L, 6402373705728000L, 121645100408832000L,
|
||||
2432902008176640000L};
|
||||
|
||||
/**
|
||||
* 提供精确的加法运算
|
||||
@ -115,9 +115,9 @@ public class NumberUtil {
|
||||
* 如果传入多个值为null或者空,则返回0
|
||||
*
|
||||
* <p>
|
||||
* 需要注意的是,在不同Locale下,数字的表示形式也是不同的,例如:<br>
|
||||
* 德国、荷兰、比利时、丹麦、意大利、罗马尼亚和欧洲大多地区使用`,`区分小数<br>
|
||||
* 也就是说,在这些国家地区,1.20表示120,而非1.2。
|
||||
* 需要注意的是,在不同Locale下,数字的表示形式也是不同的,例如:<br>
|
||||
* 德国、荷兰、比利时、丹麦、意大利、罗马尼亚和欧洲大多地区使用`,`区分小数<br>
|
||||
* 也就是说,在这些国家地区,1.20表示120,而非1.2。
|
||||
* </p>
|
||||
*
|
||||
* @param v1 被加数
|
||||
@ -133,9 +133,9 @@ public class NumberUtil {
|
||||
* 如果传入多个值为null或者空,则返回0
|
||||
*
|
||||
* <p>
|
||||
* 需要注意的是,在不同Locale下,数字的表示形式也是不同的,例如:<br>
|
||||
* 德国、荷兰、比利时、丹麦、意大利、罗马尼亚和欧洲大多地区使用`,`区分小数<br>
|
||||
* 也就是说,在这些国家地区,1.20表示120,而非1.2。
|
||||
* 需要注意的是,在不同Locale下,数字的表示形式也是不同的,例如:<br>
|
||||
* 德国、荷兰、比利时、丹麦、意大利、罗马尼亚和欧洲大多地区使用`,`区分小数<br>
|
||||
* 也就是说,在这些国家地区,1.20表示120,而非1.2。
|
||||
* </p>
|
||||
*
|
||||
* @param values 多个被加值
|
||||
@ -2242,9 +2242,9 @@ public class NumberUtil {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
try{
|
||||
try {
|
||||
return new BigDecimal(numberStr);
|
||||
} catch (Exception ignore){
|
||||
} catch (Exception ignore) {
|
||||
// 忽略解析错误
|
||||
}
|
||||
|
||||
@ -2516,14 +2516,19 @@ public class NumberUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供精确的幂运算
|
||||
* 提供精确的幂运算<br>
|
||||
* 如果n为负数,则返回1/a的-n次方,默认四舍五入
|
||||
*
|
||||
* @param number 底数
|
||||
* @param n 指数
|
||||
* @param n 指数,如果为负数,则返回1/a的-n次方
|
||||
* @return 幂的积
|
||||
* @since 4.1.0
|
||||
*/
|
||||
public static BigDecimal pow(BigDecimal number, int n) {
|
||||
if (n < 0) {
|
||||
// a的n次方,如果n为负数,则返回1/a的-n次方
|
||||
return BigDecimal.ONE.divide(pow(number, -n), 2, RoundingMode.HALF_UP);
|
||||
}
|
||||
return number.pow(n);
|
||||
}
|
||||
|
||||
@ -2565,7 +2570,7 @@ public class NumberUtil {
|
||||
return Integer.parseInt(number.substring(2), 16);
|
||||
}
|
||||
|
||||
if(StrUtil.containsIgnoreCase(number, "E")){
|
||||
if (StrUtil.containsIgnoreCase(number, "E")) {
|
||||
// 科学计数法忽略支持,科学计数法一般用于表示非常小和非常大的数字,这类数字转换为int后精度丢失,没有意义。
|
||||
throw new NumberFormatException(StrUtil.format("Unsupported int format: [{}]", number));
|
||||
}
|
||||
@ -2667,9 +2672,9 @@ public class NumberUtil {
|
||||
* 此方法不支持科学计数法
|
||||
*
|
||||
* <p>
|
||||
* 需要注意的是,在不同Locale下,数字的表示形式也是不同的,例如:<br>
|
||||
* 德国、荷兰、比利时、丹麦、意大利、罗马尼亚和欧洲大多地区使用`,`区分小数<br>
|
||||
* 也就是说,在这些国家地区,1.20表示120,而非1.2。
|
||||
* 需要注意的是,在不同Locale下,数字的表示形式也是不同的,例如:<br>
|
||||
* 德国、荷兰、比利时、丹麦、意大利、罗马尼亚和欧洲大多地区使用`,`区分小数<br>
|
||||
* 也就是说,在这些国家地区,1.20表示120,而非1.2。
|
||||
* </p>
|
||||
*
|
||||
* @param numberStr Number字符串
|
||||
@ -2681,7 +2686,7 @@ public class NumberUtil {
|
||||
if (StrUtil.startWithIgnoreCase(numberStr, "0x")) {
|
||||
// 0x04表示16进制数
|
||||
return Long.parseLong(numberStr.substring(2), 16);
|
||||
}else if(StrUtil.startWith(numberStr, '+')){
|
||||
} else if (StrUtil.startWith(numberStr, '+')) {
|
||||
// issue#I79VS7
|
||||
numberStr = StrUtil.subSuf(numberStr, 1);
|
||||
}
|
||||
@ -2870,9 +2875,9 @@ public class NumberUtil {
|
||||
*/
|
||||
public static int toInt(byte[] bytes) {
|
||||
return (bytes[0] & 0xff) << 24//
|
||||
| (bytes[1] & 0xff) << 16//
|
||||
| (bytes[2] & 0xff) << 8//
|
||||
| (bytes[3] & 0xff);
|
||||
| (bytes[1] & 0xff) << 16//
|
||||
| (bytes[2] & 0xff) << 8//
|
||||
| (bytes[3] & 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -15,6 +15,7 @@ import static cn.hutool.core.util.NumberUtil.parseFloat;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* {@link NumberUtil} 单元测试类
|
||||
@ -29,7 +30,7 @@ public class NumberUtilTest {
|
||||
final Float a = 3.15f;
|
||||
final Double b = 4.22;
|
||||
final double result = NumberUtil.add(a, b).doubleValue();
|
||||
Assert.assertEquals(7.37, result, 0);
|
||||
assertEquals(7.37, result, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -37,7 +38,7 @@ public class NumberUtilTest {
|
||||
final double a = 3.15f;//精度丢失
|
||||
final double b = 4.22;
|
||||
final double result = NumberUtil.add(a, b);
|
||||
Assert.assertEquals(7.37, result, 0.01);
|
||||
assertEquals(7.37, result, 0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -45,25 +46,25 @@ public class NumberUtilTest {
|
||||
final float a = 3.15f;
|
||||
final double b = 4.22;
|
||||
final double result = NumberUtil.add(a, b, a, b).doubleValue();
|
||||
Assert.assertEquals(14.74, result, 0);
|
||||
assertEquals(14.74, result, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addTest4() {
|
||||
final BigDecimal result = NumberUtil.add(new BigDecimal("133"), new BigDecimal("331"));
|
||||
Assert.assertEquals(new BigDecimal("464"), result);
|
||||
assertEquals(new BigDecimal("464"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addBlankTest(){
|
||||
final BigDecimal result = NumberUtil.add("123", " ");
|
||||
Assert.assertEquals(new BigDecimal("123"), result);
|
||||
assertEquals(new BigDecimal("123"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addTest5() {
|
||||
final double add = NumberUtil.add(1686036549717D, 1000D);
|
||||
Assert.assertEquals(1686036550717D, add, 0);
|
||||
assertEquals(1686036550717D, add, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -102,13 +103,13 @@ public class NumberUtilTest {
|
||||
@Test
|
||||
public void divTest() {
|
||||
final double result = NumberUtil.div(0, 1);
|
||||
Assert.assertEquals(0.0, result, 0);
|
||||
assertEquals(0.0, result, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void divBigDecimalTest() {
|
||||
final BigDecimal result = NumberUtil.div(BigDecimal.ZERO, BigDecimal.ONE);
|
||||
Assert.assertEquals(BigDecimal.ZERO, result.stripTrailingZeros());
|
||||
assertEquals(BigDecimal.ZERO, result.stripTrailingZeros());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -117,74 +118,74 @@ public class NumberUtilTest {
|
||||
// 四舍
|
||||
final String round1 = NumberUtil.roundStr(2.674, 2);
|
||||
final String round2 = NumberUtil.roundStr("2.674", 2);
|
||||
Assert.assertEquals("2.67", round1);
|
||||
Assert.assertEquals("2.67", round2);
|
||||
assertEquals("2.67", round1);
|
||||
assertEquals("2.67", round2);
|
||||
|
||||
// 五入
|
||||
final String round3 = NumberUtil.roundStr(2.675, 2);
|
||||
final String round4 = NumberUtil.roundStr("2.675", 2);
|
||||
Assert.assertEquals("2.68", round3);
|
||||
Assert.assertEquals("2.68", round4);
|
||||
assertEquals("2.68", round3);
|
||||
assertEquals("2.68", round4);
|
||||
|
||||
// 四舍六入五成双
|
||||
final String round31 = NumberUtil.roundStr(4.245, 2, RoundingMode.HALF_EVEN);
|
||||
final String round41 = NumberUtil.roundStr("4.2451", 2, RoundingMode.HALF_EVEN);
|
||||
Assert.assertEquals("4.24", round31);
|
||||
Assert.assertEquals("4.25", round41);
|
||||
assertEquals("4.24", round31);
|
||||
assertEquals("4.25", round41);
|
||||
|
||||
// 补0
|
||||
final String round5 = NumberUtil.roundStr(2.6005, 2);
|
||||
final String round6 = NumberUtil.roundStr("2.6005", 2);
|
||||
Assert.assertEquals("2.60", round5);
|
||||
Assert.assertEquals("2.60", round6);
|
||||
assertEquals("2.60", round5);
|
||||
assertEquals("2.60", round6);
|
||||
|
||||
// 补0
|
||||
final String round7 = NumberUtil.roundStr(2.600, 2);
|
||||
final String round8 = NumberUtil.roundStr("2.600", 2);
|
||||
Assert.assertEquals("2.60", round7);
|
||||
Assert.assertEquals("2.60", round8);
|
||||
assertEquals("2.60", round7);
|
||||
assertEquals("2.60", round8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void roundStrTest() {
|
||||
final String roundStr = NumberUtil.roundStr(2.647, 2);
|
||||
Assert.assertEquals(roundStr, "2.65");
|
||||
assertEquals(roundStr, "2.65");
|
||||
|
||||
final String roundStr1 = NumberUtil.roundStr(0, 10);
|
||||
Assert.assertEquals(roundStr1, "0.0000000000");
|
||||
assertEquals(roundStr1, "0.0000000000");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void roundHalfEvenTest() {
|
||||
String roundStr = NumberUtil.roundHalfEven(4.245, 2).toString();
|
||||
Assert.assertEquals(roundStr, "4.24");
|
||||
assertEquals(roundStr, "4.24");
|
||||
roundStr = NumberUtil.roundHalfEven(4.2450, 2).toString();
|
||||
Assert.assertEquals(roundStr, "4.24");
|
||||
assertEquals(roundStr, "4.24");
|
||||
roundStr = NumberUtil.roundHalfEven(4.2451, 2).toString();
|
||||
Assert.assertEquals(roundStr, "4.25");
|
||||
assertEquals(roundStr, "4.25");
|
||||
roundStr = NumberUtil.roundHalfEven(4.2250, 2).toString();
|
||||
Assert.assertEquals(roundStr, "4.22");
|
||||
assertEquals(roundStr, "4.22");
|
||||
|
||||
roundStr = NumberUtil.roundHalfEven(1.2050, 2).toString();
|
||||
Assert.assertEquals(roundStr, "1.20");
|
||||
assertEquals(roundStr, "1.20");
|
||||
roundStr = NumberUtil.roundHalfEven(1.2150, 2).toString();
|
||||
Assert.assertEquals(roundStr, "1.22");
|
||||
assertEquals(roundStr, "1.22");
|
||||
roundStr = NumberUtil.roundHalfEven(1.2250, 2).toString();
|
||||
Assert.assertEquals(roundStr, "1.22");
|
||||
assertEquals(roundStr, "1.22");
|
||||
roundStr = NumberUtil.roundHalfEven(1.2350, 2).toString();
|
||||
Assert.assertEquals(roundStr, "1.24");
|
||||
assertEquals(roundStr, "1.24");
|
||||
roundStr = NumberUtil.roundHalfEven(1.2450, 2).toString();
|
||||
Assert.assertEquals(roundStr, "1.24");
|
||||
assertEquals(roundStr, "1.24");
|
||||
roundStr = NumberUtil.roundHalfEven(1.2550, 2).toString();
|
||||
Assert.assertEquals(roundStr, "1.26");
|
||||
assertEquals(roundStr, "1.26");
|
||||
roundStr = NumberUtil.roundHalfEven(1.2650, 2).toString();
|
||||
Assert.assertEquals(roundStr, "1.26");
|
||||
assertEquals(roundStr, "1.26");
|
||||
roundStr = NumberUtil.roundHalfEven(1.2750, 2).toString();
|
||||
Assert.assertEquals(roundStr, "1.28");
|
||||
assertEquals(roundStr, "1.28");
|
||||
roundStr = NumberUtil.roundHalfEven(1.2850, 2).toString();
|
||||
Assert.assertEquals(roundStr, "1.28");
|
||||
assertEquals(roundStr, "1.28");
|
||||
roundStr = NumberUtil.roundHalfEven(1.2950, 2).toString();
|
||||
Assert.assertEquals(roundStr, "1.30");
|
||||
assertEquals(roundStr, "1.30");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -192,7 +193,7 @@ public class NumberUtilTest {
|
||||
final long c = 299792458;// 光速
|
||||
|
||||
final String format = NumberUtil.decimalFormat(",###", c);
|
||||
Assert.assertEquals("299,792,458", format);
|
||||
assertEquals("299,792,458", format);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@ -217,7 +218,7 @@ public class NumberUtilTest {
|
||||
final Double c = 467.8101;
|
||||
|
||||
final String format = NumberUtil.decimalFormat("0.00", c);
|
||||
Assert.assertEquals("467.81", format);
|
||||
assertEquals("467.81", format);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -231,11 +232,11 @@ public class NumberUtilTest {
|
||||
final double c = 299792400.543534534;
|
||||
|
||||
final String format = NumberUtil.decimalFormatMoney(c);
|
||||
Assert.assertEquals("299,792,400.54", format);
|
||||
assertEquals("299,792,400.54", format);
|
||||
|
||||
final double value = 0.5;
|
||||
final String money = NumberUtil.decimalFormatMoney(value);
|
||||
Assert.assertEquals("0.50", money);
|
||||
assertEquals("0.50", money);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -248,58 +249,58 @@ public class NumberUtilTest {
|
||||
final double a = 3.14;
|
||||
|
||||
BigDecimal bigDecimal = NumberUtil.toBigDecimal(a);
|
||||
Assert.assertEquals("3.14", bigDecimal.toString());
|
||||
assertEquals("3.14", bigDecimal.toString());
|
||||
|
||||
bigDecimal = NumberUtil.toBigDecimal("1,234.55");
|
||||
Assert.assertEquals("1234.55", bigDecimal.toString());
|
||||
assertEquals("1234.55", bigDecimal.toString());
|
||||
|
||||
bigDecimal = NumberUtil.toBigDecimal("1,234.56D");
|
||||
Assert.assertEquals("1234.56", bigDecimal.toString());
|
||||
assertEquals("1234.56", bigDecimal.toString());
|
||||
|
||||
Assert.assertEquals(new BigDecimal("9.0E+7"), NumberUtil.toBigDecimal("9.0E+7"));
|
||||
assertEquals(new BigDecimal("9.0E+7"), NumberUtil.toBigDecimal("9.0E+7"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void maxTest() {
|
||||
final int max = NumberUtil.max(5,4,3,6,1);
|
||||
Assert.assertEquals(6, max);
|
||||
assertEquals(6, max);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void minTest() {
|
||||
final int min = NumberUtil.min(5,4,3,6,1);
|
||||
Assert.assertEquals(1, min);
|
||||
assertEquals(1, min);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseIntTest() {
|
||||
int number = NumberUtil.parseInt("0xFE");
|
||||
Assert.assertEquals(254, number);
|
||||
assertEquals(254, number);
|
||||
|
||||
// 0开头
|
||||
number = NumberUtil.parseInt("010");
|
||||
Assert.assertEquals(10, number);
|
||||
assertEquals(10, number);
|
||||
|
||||
number = NumberUtil.parseInt("10");
|
||||
Assert.assertEquals(10, number);
|
||||
assertEquals(10, number);
|
||||
|
||||
number = NumberUtil.parseInt(" ");
|
||||
Assert.assertEquals(0, number);
|
||||
assertEquals(0, number);
|
||||
|
||||
number = NumberUtil.parseInt("10F");
|
||||
Assert.assertEquals(10, number);
|
||||
assertEquals(10, number);
|
||||
|
||||
number = NumberUtil.parseInt("22.4D");
|
||||
Assert.assertEquals(22, number);
|
||||
assertEquals(22, number);
|
||||
|
||||
number = NumberUtil.parseInt("22.6D");
|
||||
Assert.assertEquals(22, number);
|
||||
assertEquals(22, number);
|
||||
|
||||
number = NumberUtil.parseInt("0");
|
||||
Assert.assertEquals(0, number);
|
||||
assertEquals(0, number);
|
||||
|
||||
number = NumberUtil.parseInt(".123");
|
||||
Assert.assertEquals(0, number);
|
||||
assertEquals(0, number);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -307,13 +308,13 @@ public class NumberUtilTest {
|
||||
// from 5.4.8 issue#I23ORQ@Gitee
|
||||
// 千位分隔符去掉
|
||||
final int v1 = NumberUtil.parseInt("1,482.00");
|
||||
Assert.assertEquals(1482, v1);
|
||||
assertEquals(1482, v1);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void parseIntTest3() {
|
||||
final int v1 = NumberUtil.parseInt("d");
|
||||
Assert.assertEquals(0, v1);
|
||||
assertEquals(0, v1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -346,10 +347,10 @@ public class NumberUtilTest {
|
||||
// from 5.4.8 issue#I23ORQ@Gitee
|
||||
// 千位分隔符去掉
|
||||
final int v1 = NumberUtil.parseNumber("1,482.00").intValue();
|
||||
Assert.assertEquals(1482, v1);
|
||||
assertEquals(1482, v1);
|
||||
|
||||
final Number v2 = NumberUtil.parseNumber("1,482.00D");
|
||||
Assert.assertEquals(1482L, v2.longValue());
|
||||
assertEquals(1482L, v2.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -388,38 +389,38 @@ public class NumberUtilTest {
|
||||
public void parseHexNumberTest() {
|
||||
// 千位分隔符去掉
|
||||
final int v1 = NumberUtil.parseNumber("0xff").intValue();
|
||||
Assert.assertEquals(255, v1);
|
||||
assertEquals(255, v1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseLongTest() {
|
||||
long number = NumberUtil.parseLong("0xFF");
|
||||
Assert.assertEquals(255, number);
|
||||
assertEquals(255, number);
|
||||
|
||||
// 0开头
|
||||
number = NumberUtil.parseLong("010");
|
||||
Assert.assertEquals(10, number);
|
||||
assertEquals(10, number);
|
||||
|
||||
number = NumberUtil.parseLong("10");
|
||||
Assert.assertEquals(10, number);
|
||||
assertEquals(10, number);
|
||||
|
||||
number = NumberUtil.parseLong(" ");
|
||||
Assert.assertEquals(0, number);
|
||||
assertEquals(0, number);
|
||||
|
||||
number = NumberUtil.parseLong("10F");
|
||||
Assert.assertEquals(10, number);
|
||||
assertEquals(10, number);
|
||||
|
||||
number = NumberUtil.parseLong("22.4D");
|
||||
Assert.assertEquals(22, number);
|
||||
assertEquals(22, number);
|
||||
|
||||
number = NumberUtil.parseLong("22.6D");
|
||||
Assert.assertEquals(22, number);
|
||||
assertEquals(22, number);
|
||||
|
||||
number = NumberUtil.parseLong("0");
|
||||
Assert.assertEquals(0, number);
|
||||
assertEquals(0, number);
|
||||
|
||||
number = NumberUtil.parseLong(".123");
|
||||
Assert.assertEquals(0, number);
|
||||
assertEquals(0, number);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -488,43 +489,43 @@ public class NumberUtilTest {
|
||||
@Test
|
||||
public void factorialTest(){
|
||||
long factorial = NumberUtil.factorial(0);
|
||||
Assert.assertEquals(1, factorial);
|
||||
assertEquals(1, factorial);
|
||||
|
||||
Assert.assertEquals(1L, NumberUtil.factorial(1));
|
||||
Assert.assertEquals(1307674368000L, NumberUtil.factorial(15));
|
||||
Assert.assertEquals(2432902008176640000L, NumberUtil.factorial(20));
|
||||
assertEquals(1L, NumberUtil.factorial(1));
|
||||
assertEquals(1307674368000L, NumberUtil.factorial(15));
|
||||
assertEquals(2432902008176640000L, NumberUtil.factorial(20));
|
||||
|
||||
factorial = NumberUtil.factorial(5, 0);
|
||||
Assert.assertEquals(120, factorial);
|
||||
assertEquals(120, factorial);
|
||||
factorial = NumberUtil.factorial(5, 1);
|
||||
Assert.assertEquals(120, factorial);
|
||||
assertEquals(120, factorial);
|
||||
|
||||
Assert.assertEquals(5, NumberUtil.factorial(5, 4));
|
||||
Assert.assertEquals(2432902008176640000L, NumberUtil.factorial(20, 0));
|
||||
assertEquals(5, NumberUtil.factorial(5, 4));
|
||||
assertEquals(2432902008176640000L, NumberUtil.factorial(20, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void factorialTest2(){
|
||||
long factorial = NumberUtil.factorial(new BigInteger("0")).longValue();
|
||||
Assert.assertEquals(1, factorial);
|
||||
assertEquals(1, factorial);
|
||||
|
||||
Assert.assertEquals(1L, NumberUtil.factorial(new BigInteger("1")).longValue());
|
||||
Assert.assertEquals(1307674368000L, NumberUtil.factorial(new BigInteger("15")).longValue());
|
||||
Assert.assertEquals(2432902008176640000L, NumberUtil.factorial(20));
|
||||
assertEquals(1L, NumberUtil.factorial(new BigInteger("1")).longValue());
|
||||
assertEquals(1307674368000L, NumberUtil.factorial(new BigInteger("15")).longValue());
|
||||
assertEquals(2432902008176640000L, NumberUtil.factorial(20));
|
||||
|
||||
factorial = NumberUtil.factorial(new BigInteger("5"), new BigInteger("0")).longValue();
|
||||
Assert.assertEquals(120, factorial);
|
||||
assertEquals(120, factorial);
|
||||
factorial = NumberUtil.factorial(new BigInteger("5"), BigInteger.ONE).longValue();
|
||||
Assert.assertEquals(120, factorial);
|
||||
assertEquals(120, factorial);
|
||||
|
||||
Assert.assertEquals(5, NumberUtil.factorial(new BigInteger("5"), new BigInteger("4")).longValue());
|
||||
Assert.assertEquals(2432902008176640000L, NumberUtil.factorial(new BigInteger("20"), BigInteger.ZERO).longValue());
|
||||
assertEquals(5, NumberUtil.factorial(new BigInteger("5"), new BigInteger("4")).longValue());
|
||||
assertEquals(2432902008176640000L, NumberUtil.factorial(new BigInteger("20"), BigInteger.ZERO).longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mulTest(){
|
||||
final BigDecimal mul = NumberUtil.mul(new BigDecimal("10"), null);
|
||||
Assert.assertEquals(BigDecimal.ZERO, mul);
|
||||
assertEquals(BigDecimal.ZERO, mul);
|
||||
}
|
||||
|
||||
|
||||
@ -540,39 +541,39 @@ public class NumberUtilTest {
|
||||
@Test
|
||||
public void generateRandomNumberTest(){
|
||||
final int[] ints = NumberUtil.generateRandomNumber(10, 20, 5);
|
||||
Assert.assertEquals(5, ints.length);
|
||||
assertEquals(5, ints.length);
|
||||
final Set<?> set = Convert.convert(Set.class, ints);
|
||||
Assert.assertEquals(5, set.size());
|
||||
assertEquals(5, set.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStrTest(){
|
||||
Assert.assertEquals("1", NumberUtil.toStr(new BigDecimal("1.0000000000")));
|
||||
Assert.assertEquals("0", NumberUtil.toStr(NumberUtil.sub(new BigDecimal("9600.00000"), new BigDecimal("9600.00000"))));
|
||||
Assert.assertEquals("0", NumberUtil.toStr(NumberUtil.sub(new BigDecimal("9600.0000000000"), new BigDecimal("9600.000000"))));
|
||||
Assert.assertEquals("0", NumberUtil.toStr(new BigDecimal("9600.00000").subtract(new BigDecimal("9600.000000000"))));
|
||||
assertEquals("1", NumberUtil.toStr(new BigDecimal("1.0000000000")));
|
||||
assertEquals("0", NumberUtil.toStr(NumberUtil.sub(new BigDecimal("9600.00000"), new BigDecimal("9600.00000"))));
|
||||
assertEquals("0", NumberUtil.toStr(NumberUtil.sub(new BigDecimal("9600.0000000000"), new BigDecimal("9600.000000"))));
|
||||
assertEquals("0", NumberUtil.toStr(new BigDecimal("9600.00000").subtract(new BigDecimal("9600.000000000"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateRandomNumberTest2(){
|
||||
// 检查边界
|
||||
final int[] ints = NumberUtil.generateRandomNumber(1, 8, 7);
|
||||
Assert.assertEquals(7, ints.length);
|
||||
assertEquals(7, ints.length);
|
||||
final Set<?> set = Convert.convert(Set.class, ints);
|
||||
Assert.assertEquals(7, set.size());
|
||||
assertEquals(7, set.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toPlainNumberTest(){
|
||||
final String num = "5344.34234e3";
|
||||
final String s = new BigDecimal(num).toPlainString();
|
||||
Assert.assertEquals("5344342.34", s);
|
||||
assertEquals("5344342.34", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateBySetTest(){
|
||||
final Integer[] integers = NumberUtil.generateBySet(10, 100, 5);
|
||||
Assert.assertEquals(5, integers.length);
|
||||
assertEquals(5, integers.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -596,7 +597,7 @@ public class NumberUtilTest {
|
||||
|
||||
@Test
|
||||
public void divIntegerTest(){
|
||||
Assert.assertEquals(1001013, NumberUtil.div(100101300, (Number) 100).intValue());
|
||||
assertEquals(1001013, NumberUtil.div(100101300, (Number) 100).intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -619,21 +620,45 @@ public class NumberUtilTest {
|
||||
public void issueI79VS7Test() {
|
||||
final String value = "+0.003";
|
||||
if(NumberUtil.isNumber(value)) {
|
||||
Assert.assertEquals(0.003, NumberUtil.parseNumber(value).doubleValue(), 0);
|
||||
assertEquals(0.003, NumberUtil.parseNumber(value).doubleValue(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void issueI7R2B6Test() {
|
||||
Assert.assertEquals(61.67D,
|
||||
assertEquals(61.67D,
|
||||
NumberUtil.div(NumberUtil.mul(15858155520D, 100), 25715638272D, 2), 0.01);
|
||||
|
||||
Assert.assertEquals(61.67, NumberUtil.div(NumberUtil.mul(15858155520D, 100), 25715638272D, 2), 0.01);
|
||||
assertEquals(61.67, NumberUtil.div(NumberUtil.mul(15858155520D, 100), 25715638272D, 2), 0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void issueI7R2B6Test2() {
|
||||
final BigDecimal mul = NumberUtil.mul((Number) 15858155520D, 100.0);
|
||||
Assert.assertEquals("1585815552000", mul.toString());
|
||||
assertEquals("1585815552000", mul.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPowZero() {
|
||||
BigDecimal number = new BigDecimal("2.5");
|
||||
int exponent = 0;
|
||||
BigDecimal expected = new BigDecimal("1");
|
||||
assertEquals(expected, NumberUtil.pow(number, exponent));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPowNegative() {
|
||||
BigDecimal number = new BigDecimal("2.5");
|
||||
int exponent = -2;
|
||||
BigDecimal expected = new BigDecimal("0.16");
|
||||
assertEquals(expected, NumberUtil.pow(number, exponent));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPowSmallNumber() {
|
||||
BigDecimal number = new BigDecimal("0.1");
|
||||
int exponent = -3;
|
||||
BigDecimal expected = new BigDecimal("1000.00");
|
||||
assertEquals(expected, NumberUtil.pow(number, exponent));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user