Class NumberUtils

java.lang.Object
me.despical.commons.number.NumberUtils

public class NumberUtils extends Object
Author:
Despical

Created at 30.05.2020

  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    getDouble(String string)
    Convert a String to a double, returning zero if the conversion fails.
    static double
    getDouble(String string, double def)
    Convert a String to a double, returning a default value if the conversion fails.
    static float
    getFloat(String string)
    Convert a String to a float, returning zero if the conversion fails.
    static float
    getFloat(String string, float def)
    Convert a String to a float, returning a default value if the conversion fails.
    static int
    getInt(String string)
    Convert a String to an int, returning zero if the conversion fails.
    static int
    getInt(String string, int def)
    Convert a String to an int, returning a default value if the conversion fails.
    static long
    getLong(String string)
    Convert a String to a long, returning zero if the conversion fails.
    static long
    getLong(String string, long def)
    Convert a String to a long, returning a default value if the conversion fails.
    static long
    getShort(String string)
    Convert a String to a short, returning zero if the conversion fails.
    static long
    getShort(String string, short def)
    Convert a String to a short, returning a default value if the conversion fails.
    static boolean
    isBetween(int value, int min, int max)
    Checks if the given value is between given values.
    static boolean
    Checks if the String is primitive type double or not.
    static boolean
    Checks if the String is primitive type float.
    static boolean
    Checks if the String contains only unicode digits.
    static boolean
    Checks if the String is primitive type long or not.
    static boolean
    Checks if the String is primitive type short.
    static int
    roundInteger(int integer, int floor)
    Round an integer to nearest multiple of floor.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isInteger

      public static boolean isInteger(String str)
      Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.
      Parameters:
      str - the String to check, may be null
      Returns:
      true if only contains digits, and is non-null
    • getInt

      public static int getInt(String string)
      Convert a String to an int, returning zero if the conversion fails. If the string is null, zero is returned.
      Parameters:
      string - the string to convert, may be null
      Returns:
      the int represented by the string, or zero if conversion fails
    • getInt

      public static int getInt(String string, int def)
      Convert a String to an int, returning a default value if the conversion fails. If the string is null, the default value is returned.
      Parameters:
      string - the string to convert, may be null
      def - the default value
      Returns:
      the int represented by the string, or the default if conversion fails
    • isDouble

      public static boolean isDouble(String str)
      Checks if the String is primitive type double or not.
      Parameters:
      str - the String to check, may be null
      Returns:
      true if double, and is non-null
    • getDouble

      public static double getDouble(String string)
      Convert a String to a double, returning zero if the conversion fails. If the string is null, zero is returned.
      Parameters:
      string - the string to convert, may be null
      Returns:
      the double represented by the string, or zero if conversion fails
    • getDouble

      public static double getDouble(String string, double def)
      Convert a String to a double, returning a default value if the conversion fails. If the string is null, the default value is returned.
      Parameters:
      string - the string to convert, may be null
      def - the default value
      Returns:
      the double represented by the string, or the default if conversion fails
    • isLong

      public static boolean isLong(String str)
      Checks if the String is primitive type long or not.
      Parameters:
      str - the String to check, may be null
      Returns:
      true if long, and is non-null
    • getLong

      public static long getLong(String string)
      Convert a String to a long, returning zero if the conversion fails. If the string is null, zero is returned.
      Parameters:
      string - the string to convert, may be null
      Returns:
      the long represented by the string, or zero if conversion fails
    • getLong

      public static long getLong(String string, long def)
      Convert a String to a long, returning a default value if the conversion fails. If the string is null, the default value is returned.
      Parameters:
      string - the string to convert, may be null
      def - the default value
      Returns:
      the long represented by the string, or the default if conversion fails
    • isShort

      public static boolean isShort(String str)
      Checks if the String is primitive type short.
      Parameters:
      str - the String to check, may be null
      Returns:
      true if short, and is non-null
    • getShort

      public static long getShort(String string)
      Convert a String to a short, returning zero if the conversion fails. If the string is null, zero is returned.
      Parameters:
      string - the string to convert, may be null
      Returns:
      the short represented by the string, or zero if conversion fails
    • getShort

      public static long getShort(String string, short def)
      Convert a String to a short, returning a default value if the conversion fails. If the string is null, the default value is returned.
      Parameters:
      string - the string to convert, may be null
      def - the default value
      Returns:
      the short represented by the string, or the default if conversion fails
    • isFloat

      public static boolean isFloat(String str)
      Checks if the String is primitive type float.
      Parameters:
      str - the String to check, may be null
      Returns:
      true if float, and is non-null
    • getFloat

      public static float getFloat(String string)
      Convert a String to a float, returning zero if the conversion fails. If the string is null, zero is returned.
      Parameters:
      string - the string to convert, may be null
      Returns:
      the float represented by the string, or zero if conversion fails
    • getFloat

      public static float getFloat(String string, float def)
      Convert a String to a float, returning a default value if the conversion fails. If the string is null, the default value is returned.
      Parameters:
      string - the string to convert, may be null
      def - the default value
      Returns:
      the float represented by the string, or the default if conversion fails
    • isBetween

      public static boolean isBetween(int value, int min, int max)
      Checks if the given value is between given values.
      Parameters:
      value - the Integer to check is between values
      min - the minimum value to check given Integer
      max - the maximum value to check given Integer
      Returns:
      true if value is between min and max values
    • roundInteger

      public static int roundInteger(int integer, int floor)
      Round an integer to nearest multiple of floor.

      If 14 is the integer and 9 is the floor then method will return 18 or if the floor is same and integer is 13 it will return 9 not 18 because remainder between floor and integer is greater.

      Parameters:
      integer - int to be rounded
      floor - int to be rounded multiple of
      Returns:
      rounded integer