类 NumberUtils
java.lang.Object
cn.taketoday.util.NumberUtils
Miscellaneous utility methods for number conversion and parsing.
Mainly for internal use within the framework; consider Apache's Commons Lang for a more comprehensive suite of number utilities.
- 从以下版本开始:
- 2018-07-06 13:36:29
- 作者:
- Juergen Hoeller, Rob Harrop, Harry Yang
-
字段概要
字段修饰符和类型字段说明private static final BigIntegerprivate static final BigIntegerStandard number types (all immutable): Byte, Short, Integer, Long, BigInteger, Float, Double, BigDecimal. -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明private static longcheckedLongValue(Number number, Class<? extends Number> targetClass) Check for aBigInteger/BigDecimallong overflow before returning the given number as a long value.static <T extends Number>
TconvertNumberToTargetClass(Number number, Class<T> targetClass) Convert the given number into an instance of the given target class.private static BigIntegerdecodeBigInteger(String value) Decode aBigIntegerfrom the suppliedStringvalue.private static booleanisHexNumber(String value) Determine whether the givenvalueString indicates a hex number, i.e. needs to be passed intoInteger.decodeinstead ofInteger.valueOf, etc.static booleanIs a number?static <T extends Number>
TparseNumber(String text, Class<T> targetClass) Parse the giventextinto aNumberinstance of the given target class, using the correspondingdecode/valueOfmethod.static <T extends Number>
TparseNumber(String text, Class<T> targetClass, NumberFormat numberFormat) Parse the giventextinto aNumberinstance of the given target class, using the suppliedNumberFormat.private static voidraiseOverflowException(Number number, Class<?> targetClass) Raise an overflow exception for the given number and target class.
-
字段详细资料
-
LONG_MIN
-
LONG_MAX
-
STANDARD_NUMBER_TYPES
Standard number types (all immutable): Byte, Short, Integer, Long, BigInteger, Float, Double, BigDecimal.
-
-
构造器详细资料
-
NumberUtils
public NumberUtils()
-
-
方法详细资料
-
isNumber
Is a number?- 参数:
targetClass- the target class
-
convertNumberToTargetClass
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) throws IllegalArgumentException Convert the given number into an instance of the given target class.- 参数:
number- the number to converttargetClass- the target class to convert to- 返回:
- the converted number
- 抛出:
IllegalArgumentException- if the target class is not supported (i.e. not a standard Number subclass as included in the JDK)- 另请参阅:
-
checkedLongValue
Check for aBigInteger/BigDecimallong overflow before returning the given number as a long value.- 参数:
number- the number to converttargetClass- the target class to convert to- 返回:
- the long value, if convertible without overflow
- 抛出:
IllegalArgumentException- if there is an overflow- 另请参阅:
-
raiseOverflowException
Raise an overflow exception for the given number and target class.- 参数:
number- the number we tried to converttargetClass- the target class we tried to convert to- 抛出:
IllegalArgumentException- if there is an overflow
-
parseNumber
Parse the giventextinto aNumberinstance of the given target class, using the correspondingdecode/valueOfmethod.Trims all whitespace (leading, trailing, and in between characters) from the input
Stringbefore attempting to parse the number.Supports numbers in hex format (with leading "0x", "0X", or "#") as well.
- 参数:
text- the text to converttargetClass- the target class to parse into- 返回:
- the parsed number
- 抛出:
IllegalArgumentException- if the target class is not supported (i.e. not a standard Number subclass as included in the JDK)- 另请参阅:
-
parseNumber
public static <T extends Number> T parseNumber(String text, Class<T> targetClass, @Nullable NumberFormat numberFormat) Parse the giventextinto aNumberinstance of the given target class, using the suppliedNumberFormat.Trims the input
Stringbefore attempting to parse the number.- 参数:
text- the text to converttargetClass- the target class to parse intonumberFormat- theNumberFormatto use for parsing (ifnull, this method falls back toparseNumber(String, Class))- 返回:
- the parsed number
- 抛出:
IllegalArgumentException- if the target class is not supported (i.e. not a standard Number subclass as included in the JDK)- 另请参阅:
-
isHexNumber
Determine whether the givenvalueString indicates a hex number, i.e. needs to be passed intoInteger.decodeinstead ofInteger.valueOf, etc. -
decodeBigInteger
- 另请参阅:
-