public abstract class NumberUtils extends Object
Modifier and Type | Field and Description |
---|---|
static Set<Class<?>> |
STANDARD_NUMBER_TYPES
Standard number types (all immutable):
Byte, Short, Integer, Long, BigInteger, Float, Double, BigDecimal.
|
Constructor and Description |
---|
NumberUtils() |
Modifier and Type | Method and Description |
---|---|
static <T extends Number> |
convertNumberToTargetClass(Number number,
Class<T> targetClass)
Convert the given number into an instance of the given target class.
|
static <T extends Number> |
parseNumber(String text,
Class<T> targetClass)
Parse the given text into a number instance of the given target class,
using the corresponding
decode / valueOf methods. |
static <T extends Number> |
parseNumber(String text,
Class<T> targetClass,
NumberFormat numberFormat)
Parse the given text into a number instance of the given target class,
using the given NumberFormat.
|
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) throws IllegalArgumentException
number
- the number to converttargetClass
- the target class to convert toIllegalArgumentException
- 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
public static <T extends Number> T parseNumber(String text, Class<T> targetClass)
decode
/ valueOf
methods.
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 intoIllegalArgumentException
- 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.BigDecimal(String)
public static <T extends Number> T parseNumber(String text, Class<T> targetClass, NumberFormat numberFormat)
String
before attempting to parse the number.text
- the text to converttargetClass
- the target class to parse intonumberFormat
- the NumberFormat to use for parsing (if null
,
this method falls back to parseNumber(String, Class)
)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)