mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
Merge pull request #1286 from lazzman/v5-dev
[新特性] NumberUtil类添加检查值是否在范围区间方法
This commit is contained in:
commit
9206bbf1bd
@ -2493,6 +2493,58 @@ public class NumberUtil {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查value是否在[min,max]范围内
|
||||||
|
*
|
||||||
|
* @param min 最小值
|
||||||
|
* @param max 最大值
|
||||||
|
* @param value 被检查值
|
||||||
|
* @return 检查结果,范围内将返回true,否则返回false
|
||||||
|
* @since 5.5.4
|
||||||
|
*/
|
||||||
|
public static boolean isBetween(int min, int max, int value) {
|
||||||
|
return value >= min && value <= max;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查value是否在[min,max]范围内
|
||||||
|
*
|
||||||
|
* @param min 最小值
|
||||||
|
* @param max 最大值
|
||||||
|
* @param value 被检查值
|
||||||
|
* @return 检查结果,范围内将返回true,否则返回false
|
||||||
|
* @since 5.5.4
|
||||||
|
*/
|
||||||
|
public static boolean isBetween(long min, long max, long value) {
|
||||||
|
return value >= min && value <= max;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查value是否在[min,max]范围内
|
||||||
|
*
|
||||||
|
* @param min 最小值
|
||||||
|
* @param max 最大值
|
||||||
|
* @param value 被检查值
|
||||||
|
* @return 检查结果,范围内将返回true,否则返回false
|
||||||
|
* @since 5.5.4
|
||||||
|
*/
|
||||||
|
public static boolean isBetween(float min, float max, float value) {
|
||||||
|
return value >= min && value <= max;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查value是否在[min,max]范围内
|
||||||
|
*
|
||||||
|
* @param min 最小值
|
||||||
|
* @param max 最大值
|
||||||
|
* @param value 被检查值
|
||||||
|
* @return 检查结果,范围内将返回true,否则返回false
|
||||||
|
* @since 5.5.4
|
||||||
|
*/
|
||||||
|
public static boolean isBetween(double min, double max, double value) {
|
||||||
|
return value >= min && value <= max;
|
||||||
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------- Private method start
|
// ------------------------------------------------------------------------------------------- Private method start
|
||||||
private static int mathSubnode(int selectNum, int minNum) {
|
private static int mathSubnode(int selectNum, int minNum) {
|
||||||
if (selectNum == minNum) {
|
if (selectNum == minNum) {
|
||||||
|
@ -303,4 +303,12 @@ public class NumberUtilTest {
|
|||||||
Assert.assertEquals("0", NumberUtil.toStr(NumberUtil.sub(new BigDecimal("9600.0000000000"), new BigDecimal("9600.000000"))));
|
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"))));
|
Assert.assertEquals("0", NumberUtil.toStr(new BigDecimal("9600.00000").subtract(new BigDecimal("9600.000000000"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isBetweenTest() {
|
||||||
|
Assert.assertTrue(NumberUtil.isBetween(0, 1, 0));
|
||||||
|
Assert.assertTrue(NumberUtil.isBetween(0l, 1l, 1l));
|
||||||
|
Assert.assertTrue(NumberUtil.isBetween(0.1f, 0.2f, 0.19f));
|
||||||
|
Assert.assertTrue(NumberUtil.isBetween(0.1, 0.2, 0.19));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user