类 NumberUtils
- java.lang.Object
- org.springframework.util.NumberUtils
public abstract class NumberUtils extends Object
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.
- 从以下版本开始:
- 1.1.2
- 作者:
- Juergen Hoeller, Rob Harrop
字段概要
字段 修饰符和类型 字段 说明 static Set<Class<?>>
STANDARD_NUMBER_TYPES
Standard number types (all immutable): Byte, Short, Integer, Long, BigInteger, Float, Double, BigDecimal.
构造器概要
构造器 构造器 说明 NumberUtils()
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static <T extends Number>
TconvertNumberToTargetClass(Number number, Class<T> targetClass)
Convert the given number into an instance of the given target class.static <T extends Number>
TparseNumber(String text, Class<T> targetClass)
Parse the giventext
into aNumber
instance of the given target class, using the correspondingdecode
/valueOf
method.static <T extends Number>
TparseNumber(String text, Class<T> targetClass, NumberFormat numberFormat)
Parse the giventext
into aNumber
instance of the given target class, using the suppliedNumberFormat
.
字段详细资料
STANDARD_NUMBER_TYPES
public static final Set<Class<?>> STANDARD_NUMBER_TYPES
Standard number types (all immutable): Byte, Short, Integer, Long, BigInteger, Float, Double, BigDecimal.
构造器详细资料
NumberUtils
public NumberUtils()
方法详细资料
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)- 另请参阅:
Byte
,Short
,Integer
,Long
,BigInteger
,Float
,Double
,BigDecimal
parseNumber
public static <T extends Number> T parseNumber(String text, Class<T> targetClass)
Parse the giventext
into aNumber
instance of the given target class, using the correspondingdecode
/valueOf
method.Trims the input
String
before 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)- 另请参阅:
Byte.decode(java.lang.String)
,Short.decode(java.lang.String)
,Integer.decode(java.lang.String)
,Long.decode(java.lang.String)
,decodeBigInteger(String)
,Float.valueOf(java.lang.String)
,Double.valueOf(java.lang.String)
,BigDecimal(String)
parseNumber
public static <T extends Number> T parseNumber(String text, Class<T> targetClass, NumberFormat numberFormat)
Parse the giventext
into aNumber
instance of the given target class, using the suppliedNumberFormat
.Trims the input
String
before attempting to parse the number.- 参数:
text
- the text to converttargetClass
- the target class to parse intonumberFormat
- theNumberFormat
to 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)- 另请参阅:
NumberFormat.parse(java.lang.String, java.text.ParsePosition)
,convertNumberToTargetClass(java.lang.Number, java.lang.Class<T>)
,parseNumber(String, Class)