Package org.springframework.util
Class NumberUtils
java.lang.Object
org.springframework.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.
- Since:
 - 1.1.2
 - Author:
 - Juergen Hoeller, Rob Harrop
 
- 
Field Summary
FieldsModifier and TypeFieldDescriptionStandard number types (all immutable): Byte, Short, Integer, Long, BigInteger, Float, Double, BigDecimal. - 
Constructor Summary
Constructors - 
Method Summary
Modifier and TypeMethodDescriptionstatic <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 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. 
- 
Field Details
- 
STANDARD_NUMBER_TYPES
Standard number types (all immutable): Byte, Short, Integer, Long, BigInteger, Float, Double, BigDecimal. 
 - 
 - 
Constructor Details
- 
NumberUtils
public NumberUtils() 
 - 
 - 
Method Details
- 
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.- Parameters:
 number- the number to converttargetClass- the target class to convert to- Returns:
 - the converted number
 - Throws:
 IllegalArgumentException- if the target class is not supported (i.e. not a standard Number subclass as included in the JDK)- See Also:
 
 - 
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.
- Parameters:
 text- the text to converttargetClass- the target class to parse into- Returns:
 - the parsed number
 - Throws:
 IllegalArgumentException- if the target class is not supported (i.e. not a standard Number subclass as included in the JDK)- See Also:
 
 - 
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.- Parameters:
 text- the text to converttargetClass- the target class to parse intonumberFormat- theNumberFormatto use for parsing (ifnull, this method falls back toparseNumber(String, Class))- Returns:
 - the parsed number
 - Throws:
 IllegalArgumentException- if the target class is not supported (i.e. not a standard Number subclass as included in the JDK)- See Also:
 
 
 -