Package me.despical.commons.number
Class NumberUtils
java.lang.Object
me.despical.commons.number.NumberUtils
- Author:
- Despical
Created at 30.05.2020
-
Method Summary
Modifier and TypeMethodDescriptionstatic doubleConvert a String to a double, returning zero if the conversion fails.static doubleConvert a String to a double, returning a default value if the conversion fails.static floatConvert a String to a float, returning zero if the conversion fails.static floatConvert a String to a float, returning a default value if the conversion fails.static intConvert a String to an int, returning zero if the conversion fails.static intConvert a String to an int, returning a default value if the conversion fails.static longConvert a String to a long, returning zero if the conversion fails.static longConvert a String to a long, returning a default value if the conversion fails.static longConvert a String to a short, returning zero if the conversion fails.static longConvert a String to a short, returning a default value if the conversion fails.static booleanisBetween(int value, int min, int max) Checks if the given value is between given values.static booleanChecks if the String is primitive type double or not.static booleanChecks if the String is primitive type float.static booleanChecks if the String contains only unicode digits.static booleanChecks if the String is primitive type long or not.static booleanChecks if the String is primitive type short.static introundInteger(int integer, int floor) Round an integer to nearest multiple of floor.
-
Method Details
-
isInteger
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
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
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 nulldef- the default value- Returns:
- the int represented by the string, or the default if conversion fails
-
isDouble
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
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
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 nulldef- the default value- Returns:
- the double represented by the string, or the default if conversion fails
-
isLong
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
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
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 nulldef- the default value- Returns:
- the long represented by the string, or the default if conversion fails
-
isShort
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
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
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 nulldef- the default value- Returns:
- the short represented by the string, or the default if conversion fails
-
isFloat
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
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
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 nulldef- 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 valuesmin- the minimum value to check given Integermax- 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 roundedfloor- int to be rounded multiple of- Returns:
- rounded integer
-