Top Description Fields Constructors Methods
org.springframework.util

public abstract Class NumberUtils

extends Object
Class Inheritance
Imports
java.math.BigDecimal, .BigInteger, java.text.DecimalFormat, .NumberFormat, .ParseException, java.util.Set, org.springframework.lang.Nullable

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.

Authors
Juergen Hoeller, Rob Harrop
Since
1.1.2

Field Summary

Modifier and TypeField and Description
private static final BigInteger
private static final BigInteger
public static final Set<Class<?>>
STANDARD_NUMBER_TYPES

Standard number types (all immutable): Byte, Short, Integer, Long, BigInteger, Float, Double, BigDecimal.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static long

Returns:

the long value, if convertible without overflow
checkedLongValue
(Number
the number to convert
number
,
Class<? extends Number>
the target class to convert to
targetClass
)

Check for a BigInteger/BigDecimal long overflow before returning the given number as a long value.

public static <T extends Number> T

Returns:

the converted number
convertNumberToTargetClass
(Number
the number to convert
number
,
Class<T>
the target class to convert to
targetClass
)

Convert the given number into an instance of the given target class.

private static BigInteger
decodeBigInteger(String value)

Decode a java.math.BigInteger from the supplied String value.

private static boolean
isHexNumber(String value)

Determine whether the given value String indicates a hex number, i.e. needs to be passed into Integer.decode instead of Integer.valueOf, etc.

public static <T extends Number> T

Returns:

the parsed number
parseNumber
(String
the text to convert
text
,
Class<T>
the target class to parse into
targetClass
)

Parse the given text into a Number instance of the given target class, using the corresponding decode / valueOf method.

public static <T extends Number> T

Returns:

the parsed number
parseNumber
(String
the text to convert
text
,
Class<T>
the target class to parse into
targetClass
,
NumberFormat
the NumberFormat to use for parsing (if null, this method falls back to parseNumber(String, Class))
numberFormat
)

Parse the given text into a Number instance of the given target class, using the supplied NumberFormat.

private static void
raiseOverflowException(Number
the number we tried to convert
number
,
Class<?>
the target class we tried to convert to
targetClass
)

Raise an overflow exception for the given number and target class.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

LONG_MAXback to summary
private static final BigInteger LONG_MAX
LONG_MINback to summary
private static final BigInteger LONG_MIN
STANDARD_NUMBER_TYPESback to summary
public static final Set<Class<?>> STANDARD_NUMBER_TYPES

Standard number types (all immutable): Byte, Short, Integer, Long, BigInteger, Float, Double, BigDecimal.

Constructor Detail

NumberUtilsback to summary
public NumberUtils()

Method Detail

checkedLongValueback to summary
private static long checkedLongValue(Number number, Class<? extends Number> targetClass)

Check for a BigInteger/BigDecimal long overflow before returning the given number as a long value.

Parameters
number:Number

the number to convert

targetClass:Class<? extends Number>

the target class to convert to

Returns:long

the long value, if convertible without overflow

Exceptions
IllegalArgumentException:
if there is an overflow
See Also
raiseOverflowException
convertNumberToTargetClassback to summary
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:Number

the number to convert

targetClass:Class<T>

the target class to convert to

Returns:T

the converted number

Annotations
@SuppressWarnings:unchecked
Exceptions
IllegalArgumentException:
if the target class is not supported (i.e. not a standard Number subclass as included in the JDK)
See Also
java.lang.Byte, java.lang.Short, java.lang.Integer, java.lang.Long, java.math.BigInteger, java.lang.Float, java.lang.Double, java.math.BigDecimal
decodeBigIntegerback to summary
private static BigInteger decodeBigInteger(String value)

Decode a java.math.BigInteger from the supplied String value.

Supports decimal, hex, and octal notation.

See Also
BigInteger#BigInteger(String, int)
isHexNumberback to summary
private static boolean isHexNumber(String value)

Determine whether the given value String indicates a hex number, i.e. needs to be passed into Integer.decode instead of Integer.valueOf, etc.

parseNumberback to summary
public static <T extends Number> T parseNumber(String text, Class<T> targetClass)

Parse the given text into a Number instance of the given target class, using the corresponding decode / valueOf method.

Trims all whitespace (leading, trailing, and in between characters) from the input String before attempting to parse the number.

Supports numbers in hex format (with leading "0x", "0X", or "#") as well.

Parameters
text:String

the text to convert

targetClass:Class<T>

the target class to parse into

Returns:T

the parsed number

Annotations
@SuppressWarnings:unchecked
Exceptions
IllegalArgumentException:
if the target class is not supported (i.e. not a standard Number subclass as included in the JDK)
See Also
Byte#decode, Short#decode, Integer#decode, Long#decode, decodeBigInteger(String), Float#valueOf, Double#valueOf, java.math.BigDecimal#BigDecimal(String)
parseNumberback to summary
public static <T extends Number> T parseNumber(String text, Class<T> targetClass, NumberFormat numberFormat)

Parse the given text into a Number instance of the given target class, using the supplied NumberFormat.

Trims the input String before attempting to parse the number.

Parameters
text:String

the text to convert

targetClass:Class<T>

the target class to parse into

numberFormat:NumberFormat

the NumberFormat to use for parsing (if null, this method falls back to parseNumber(String, Class))

Returns:T

the parsed number

Exceptions
IllegalArgumentException:
if the target class is not supported (i.e. not a standard Number subclass as included in the JDK)
See Also
java.text.NumberFormat#parse, convertNumberToTargetClass, parseNumber(String, Class)
raiseOverflowExceptionback to summary
private static void raiseOverflowException(Number number, Class<?> targetClass)

Raise an overflow exception for the given number and target class.

Parameters
number:Number

the number we tried to convert

targetClass:Class<?>

the target class we tried to convert to

Exceptions
IllegalArgumentException:
if there is an overflow