org.springframework.util

Class NumberUtils

  • java.lang.Object
    • org.springframework.util.NumberUtils


  • public abstract class NumberUtils
    extends java.lang.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.

    Since:
    1.1.2
    Author:
    Juergen Hoeller, Rob Harrop
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      static java.util.Set<java.lang.Class<?>> STANDARD_NUMBER_TYPES
      Standard number types (all immutable): Byte, Short, Integer, Long, BigInteger, Float, Double, BigDecimal.
    • Constructor Summary

      Constructors 
      Constructor and Description
      NumberUtils() 
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      static <T extends java.lang.Number>
      T
      convertNumberToTargetClass(java.lang.Number number, java.lang.Class<T> targetClass)
      Convert the given number into an instance of the given target class.
      static <T extends java.lang.Number>
      T
      parseNumber(java.lang.String text, java.lang.Class<T> targetClass)
      Parse the given text into a Number instance of the given target class, using the corresponding decode / valueOf method.
      static <T extends java.lang.Number>
      T
      parseNumber(java.lang.String text, java.lang.Class<T> targetClass, java.text.NumberFormat numberFormat)
      Parse the given text into a Number instance of the given target class, using the supplied NumberFormat.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • STANDARD_NUMBER_TYPES

        public static final java.util.Set<java.lang.Class<?>> STANDARD_NUMBER_TYPES
        Standard number types (all immutable): Byte, Short, Integer, Long, BigInteger, Float, Double, BigDecimal.
    • Constructor Detail

      • NumberUtils

        public NumberUtils()
    • Method Detail

      • convertNumberToTargetClass

        public static <T extends java.lang.Number> T convertNumberToTargetClass(java.lang.Number number,
                                                                                java.lang.Class<T> targetClass)
                                                                         throws java.lang.IllegalArgumentException
        Convert the given number into an instance of the given target class.
        Parameters:
        number - the number to convert
        targetClass - the target class to convert to
        Returns:
        the converted number
        Throws:
        java.lang.IllegalArgumentException - if the target class is not supported (i.e. not a standard Number subclass as included in the JDK)
        See Also:
        Byte, Short, Integer, Long, BigInteger, Float, Double, BigDecimal
      • parseNumber

        public static <T extends java.lang.Number> T parseNumber(java.lang.String text,
                                                                 java.lang.Class<T> targetClass)
        Parse the given text into a Number instance of the given target class, using the corresponding decode / valueOf method.

        Trims the input String before attempting to parse the number.

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

        Parameters:
        text - the text to convert
        targetClass - the target class to parse into
        Returns:
        the parsed number
        Throws:
        java.lang.IllegalArgumentException - if the target class is not supported (i.e. not a standard Number subclass as included in the JDK)
        See Also:
        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)
      • parseNumber

        public static <T extends java.lang.Number> T parseNumber(java.lang.String text,
                                                                 java.lang.Class<T> targetClass,
                                                                 @Nullable
                                                                 java.text.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 - the text to convert
        targetClass - the target class to parse into
        numberFormat - the NumberFormat to use for parsing (if null, this method falls back to parseNumber(String, Class))
        Returns:
        the parsed number
        Throws:
        java.lang.IllegalArgumentException - if the target class is not supported (i.e. not a standard Number subclass as included in the JDK)
        See Also:
        NumberFormat.parse(java.lang.String, java.text.ParsePosition), convertNumberToTargetClass(java.lang.Number, java.lang.Class<T>), parseNumber(String, Class)