public final class NumberUtils extends Object
Provides extra functionality for Java Number classes.
| 构造器和说明 |
|---|
NumberUtils()
NumberUtils instances should NOT be constructed in standard programming. |
| 限定符和类型 | 方法和说明 |
|---|---|
static Number |
add(Number obj1,
Number obj2)
加
|
static Number |
aliquot(Number obj1,
Number obj2)
整除
|
static Number |
and(Number obj1,
Number obj2)
与
|
static double |
between(double value,
double min,
double max)
Gets the between num of min,max
double values. |
static float |
between(float value,
float min,
float max)
Gets the between num of min,max
float values. |
static int |
between(int value,
int min,
int max)
Gets the between num of min,max
int values. |
static long |
between(long value,
long min,
long max)
Gets the between num of min,max
long values. |
static int |
compare(double lhs,
double rhs)
Compares two
doubles for order. |
static int |
compare(float lhs,
float rhs)
Compares two floats for order.
|
static BigDecimal |
createBigDecimal(String val)
Convert a
String to a BigDecimal. |
static BigInteger |
createBigInteger(String val)
Convert a
String to a BigInteger. |
static Double |
createDouble(String val)
Convert a
String to a Double. |
static Float |
createFloat(String val)
Convert a
String to a Float. |
static Integer |
createInteger(String val)
Convert a
String to a Integer, handling
hex and octal notations. |
static Long |
createLong(String val)
Convert a
String to a Long. |
static Number |
createNumber(String val)
Turns a string value into a java.lang.Number.
|
static BigInteger |
createUnsignedLong(long value)
Convert a
long to a Unsigned Long. |
static Number |
divide(Number obj1,
Number obj2,
int precision,
RoundingMode roundingEnum)
除
|
static boolean |
eq(Number obj1,
Number obj2)
相等
|
static int |
getNumericType(Object value) |
static boolean |
gt(Number obj1,
Number obj2)
大于
|
static boolean |
gteq(Number obj1,
Number obj2)
大于等于
|
static boolean |
isBoolean(Object object) |
static boolean |
isByteNumber(Object number) |
static boolean |
isByteType(Class<?> targetType) |
static boolean |
isCharacter(Object number) |
static boolean |
isDecimal(Number tester)
测试是否为一个小数
|
static boolean |
isDigits(String str)
Checks whether the
String contains only
digit characters. |
static boolean |
isDoubleNumber(Object number) |
static boolean |
isDoubleType(Class<?> targetType) |
static boolean |
isFloatNumber(Object number) |
static boolean |
isFloatType(Class<?> targetType) |
static boolean |
isIntegerNumber(Object number) |
static boolean |
isIntType(Class<?> targetType) |
static boolean |
isLongNumber(Object number) |
static boolean |
isLongType(Class<?> targetType) |
static boolean |
isNumber(Object object) |
static boolean |
isNumber(String str)
Checks whether the String a valid Java number.
|
static boolean |
isShortNumber(Object number) |
static boolean |
isShortType(Class<?> targetType) |
static boolean |
lt(Number obj1,
Number obj2)
小于
|
static boolean |
lteq(Number obj1,
Number obj2)
小于等于
|
static int |
maximum(int a,
int b,
int c)
Gets the maximum of three
int values. |
static long |
maximum(long a,
long b,
long c)
Gets the maximum of three
long values. |
static int |
minimum(int a,
int b,
int c)
Gets the minimum of three
int values. |
static long |
minimum(long a,
long b,
long c)
Gets the minimum of three
long values. |
static Number |
mod(Number obj1,
Number obj2)
求余
|
static Number |
multiply(Number obj1,
Number obj2)
乘
|
static Number |
negate(Number obj)
取反,相当于:value * -1
|
static Number |
or(Number obj1,
Number obj2)
或
|
static BigDecimal |
round(BigDecimal number,
int scale)
|
static BigDecimal |
round(BigDecimal number,
int scale,
RoundingMode roundingMode)
保留固定位数小数
例如保留四位小数:123.456789 => 123.4567 |
static BigDecimal |
round(double v,
int scale)
|
static BigDecimal |
round(double v,
int scale,
RoundingMode roundingMode)
保留固定位数小数
例如保留四位小数:123.456789 => 123.4567 |
static BigDecimal |
round(String numberStr,
int scale)
|
static BigDecimal |
round(String numberStr,
int scale,
RoundingMode roundingMode)
保留固定位数小数
例如保留四位小数:123.456789 => 123.4567 |
static BigDecimal |
roundDown(BigDecimal value,
int scale)
保留固定小数位数,舍去多余位数
|
static BigDecimal |
roundDown(Number number,
int scale)
保留固定小数位数,舍去多余位数
|
static BigDecimal |
roundHalfEven(BigDecimal value,
int scale)
四舍六入五成双计算法
四舍六入五成双是一种比较精确比较科学的计数保留法,是一种数字修约规则。
|
static BigDecimal |
roundHalfEven(Number number,
int scale)
四舍六入五成双计算法
四舍六入五成双是一种比较精确比较科学的计数保留法,是一种数字修约规则。
|
static String |
roundStr(double v,
int scale)
|
static String |
roundStr(double v,
int scale,
RoundingMode roundingMode)
保留固定位数小数
例如保留四位小数:123.456789 => 123.4567 |
static String |
roundStr(String numberStr,
int scale)
|
static String |
roundStr(String numberStr,
int scale,
RoundingMode roundingMode)
保留固定位数小数
例如保留四位小数:123.456789 => 123.4567 |
static Number |
shiftLeft(Number obj1,
Number obj2)
左位移
|
static Number |
shiftRight(Number obj1,
Number obj2)
右位移
|
static Number |
shiftRightWithUnsigned(Number obj1,
Number obj2)
无符号右位移
|
static int |
stringToInt(String str)
Convert a
String to an int, returning
zero if the conversion fails. |
static int |
stringToInt(String str,
int defaultValue)
Convert a
String to an int, returning a
default value if the conversion fails. |
static Number |
subtract(Number obj1,
Number obj2)
减
|
static Number |
xor(Number obj1,
Number obj2)
异或
|
public NumberUtils()
NumberUtils instances should NOT be constructed in standard programming.
Instead, the class should be used as NumberUtils.stringToInt("6");.
This constructor is public to permit tools that require a JavaBean instance to operate.
public static int stringToInt(String str)
Convert a String to an int, returning
zero if the conversion fails.
str - the string to convertzero if
conversion failspublic static int stringToInt(String str, int defaultValue)
Convert a String to an int, returning a
default value if the conversion fails.
str - the string to convertdefaultValue - the default valuepublic static Number createNumber(String val) throws NumberFormatException
Turns a string value into a java.lang.Number.
First, the value is examined for a type qualifier on the end
('f','F','d','D','l','L'). If it is found, it starts
trying to create successively larger types from the type specified
until one is found that can hold the value.
If a type specifier is not found, it will check for a decimal point
and then try successively larger types from Integer to
BigInteger and from Float to
BigDecimal.
If the string starts with 0x or -0x, it
will be interpreted as a hexadecimal integer. Values with leading
0's will not be interpreted as octal.
val - String containing a numberNumberFormatException - if the value cannot be convertedpublic static Float createFloat(String val)
Convert a String to a Float.
val - a String to convertFloatNumberFormatException - if the value cannot be convertedpublic static Double createDouble(String val)
Convert a String to a Double.
val - a String to convertDoubleNumberFormatException - if the value cannot be convertedpublic static Integer createInteger(String val)
Convert a String to a Integer, handling
hex and octal notations.
val - a String to convertIntegerNumberFormatException - if the value cannot be convertedpublic static Long createLong(String val)
Convert a String to a Long.
val - a String to convertLongNumberFormatException - if the value cannot be convertedpublic static BigInteger createUnsignedLong(long value)
Convert a long to a Unsigned Long.
value - a String to convertLongNumberFormatException - if the value cannot be convertedpublic static BigInteger createBigInteger(String val)
Convert a String to a BigInteger.
val - a String to convertBigIntegerNumberFormatException - if the value cannot be convertedpublic static BigDecimal createBigDecimal(String val)
Convert a String to a BigDecimal.
val - a String to convertBigDecimalNumberFormatException - if the value cannot be convertedpublic static long minimum(long a,
long b,
long c)
Gets the minimum of three long values.
a - value 1b - value 2c - value 3public static int minimum(int a,
int b,
int c)
Gets the minimum of three int values.
a - value 1b - value 2c - value 3public static long maximum(long a,
long b,
long c)
Gets the maximum of three long values.
a - value 1b - value 2c - value 3public static int maximum(int a,
int b,
int c)
Gets the maximum of three int values.
a - value 1b - value 2c - value 3public static long between(long value,
long min,
long max)
Gets the between num of min,max long values.
value - value 1min - value 2max - value 3public static int between(int value,
int min,
int max)
Gets the between num of min,max int values.
value - value 1min - value 2max - value 3public static float between(float value,
float min,
float max)
Gets the between num of min,max float values.
value - value 1min - value 2max - value 3public static double between(double value,
double min,
double max)
Gets the between num of min,max double values.
value - value 1min - value 2max - value 3public static int compare(double lhs,
double rhs)
Compares two doubles for order.
This method is more comprehensive than the standard Java greater than, less than and equals operators.
-1 if the first value is less than the second.
+1 if the first value is greater than the second.
0 if the values are equal.
The ordering is as follows, largest to smallest:
Comparing NaN with NaN will
return 0.
lhs - the first doublerhs - the second double-1 if lhs is less, +1 if greater,
0 if equal to rhspublic static int compare(float lhs,
float rhs)
Compares two floats for order.
This method is more comprehensive than the standard Java greater than, less than and equals operators.
-1 if the first value is less than the second.
+1 if the first value is greater than the second.
0 if the values are equal.
The ordering is as follows, largest to smallest:
Comparing NaN with NaN will return
0.
lhs - the first floatrhs - the second float-1 if lhs is less, +1 if greater,
0 if equal to rhspublic static boolean isDigits(String str)
Checks whether the String contains only
digit characters.
Null and empty String will return
false.
str - the String to checktrue if str contains only unicode numericpublic static boolean isNumber(String str)
Checks whether the String a valid Java number.
Valid numbers include hexadecimal marked with the 0x
qualifier, scientific notation and numbers marked with a type
qualifier (e.g. 123L).
Null and empty String will return
false.
str - the String to checktrue if the string is a correctly formatted numberpublic static boolean isByteType(Class<?> targetType)
public static boolean isShortType(Class<?> targetType)
public static boolean isIntType(Class<?> targetType)
public static boolean isLongType(Class<?> targetType)
public static boolean isFloatType(Class<?> targetType)
public static boolean isDoubleType(Class<?> targetType)
public static boolean isNumber(Object object)
public static boolean isBoolean(Object object)
public static boolean isByteNumber(Object number)
public static boolean isShortNumber(Object number)
public static boolean isIntegerNumber(Object number)
public static boolean isCharacter(Object number)
public static boolean isLongNumber(Object number)
public static boolean isFloatNumber(Object number)
public static boolean isDoubleNumber(Object number)
public static int getNumericType(Object value)
public static Number divide(Number obj1, Number obj2, int precision, RoundingMode roundingEnum)
public static boolean isDecimal(Number tester)
public static BigDecimal round(double v, int scale)
v - 值scale - 保留小数位数public static String roundStr(double v, int scale)
v - 值scale - 保留小数位数public static BigDecimal round(String numberStr, int scale)
numberStr - 数字值的字符串表现形式scale - 保留小数位数public static BigDecimal round(BigDecimal number, int scale)
number - 数字值scale - 保留小数位数public static String roundStr(String numberStr, int scale)
numberStr - 数字值的字符串表现形式scale - 保留小数位数public static BigDecimal round(double v, int scale, RoundingMode roundingMode)
v - 值scale - 保留小数位数roundingMode - 保留小数的模式 RoundingModepublic static String roundStr(double v, int scale, RoundingMode roundingMode)
v - 值scale - 保留小数位数roundingMode - 保留小数的模式 RoundingModepublic static BigDecimal round(String numberStr, int scale, RoundingMode roundingMode)
numberStr - 数字值的字符串表现形式scale - 保留小数位数,如果传入小于0,则默认0roundingMode - 保留小数的模式 RoundingMode,如果传入null则默认四舍五入public static BigDecimal round(BigDecimal number, int scale, RoundingMode roundingMode)
number - 数字值scale - 保留小数位数,如果传入小于0,则默认0roundingMode - 保留小数的模式 RoundingMode,如果传入null则默认四舍五入public static String roundStr(String numberStr, int scale, RoundingMode roundingMode)
numberStr - 数字值的字符串表现形式scale - 保留小数位数roundingMode - 保留小数的模式 RoundingModepublic static BigDecimal roundHalfEven(Number number, int scale)
四舍六入五成双是一种比较精确比较科学的计数保留法,是一种数字修约规则。
算法规则: 四舍六入五考虑, 五后非零就进一, 五后皆零看奇偶, 五前为偶应舍去, 五前为奇要进一。
number - 需要科学计算的数据scale - 保留的小数位public static BigDecimal roundHalfEven(BigDecimal value, int scale)
四舍六入五成双是一种比较精确比较科学的计数保留法,是一种数字修约规则。
算法规则: 四舍六入五考虑, 五后非零就进一, 五后皆零看奇偶, 五前为偶应舍去, 五前为奇要进一。
value - 需要科学计算的数据scale - 保留的小数位public static BigDecimal roundDown(Number number, int scale)
number - 需要科学计算的数据scale - 保留的小数位public static BigDecimal roundDown(BigDecimal value, int scale)
value - 需要科学计算的数据scale - 保留的小数位Copyright © 2022–2023. All rights reserved.