Class NumberUtils
Provides extra functionality for Java Number classes.
- Since:
- 2.0
- Version:
- $Id: NumberUtils.java 1056853 2011-01-09 01:07:04Z niallp $
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ByteReusable Byte constant for minus one.static final ByteReusable Byte constant for one.static final ByteReusable Byte constant for zero.static final DoubleReusable Double constant for minus one.static final DoubleReusable Double constant for one.static final DoubleReusable Double constant for zero.static final FloatReusable Float constant for minus one.static final FloatReusable Float constant for one.static final FloatReusable Float constant for zero.static final IntegerReusable Integer constant for minus one.static final IntegerReusable Integer constant for one.static final IntegerReusable Integer constant for zero.static final LongReusable Long constant for minus one.static final LongReusable Long constant for one.static final LongReusable Long constant for zero.static final ShortReusable Short constant for minus one.static final ShortReusable Short constant for one.static final ShortReusable Short constant for zero. -
Constructor Summary
ConstructorsConstructorDescriptionNumberUtilsinstances should NOT be constructed in standard programming. -
Method Summary
Modifier and TypeMethodDescriptionstatic intcompare(double lhs, double rhs) Compares twodoublesfor order.static intcompare(float lhs, float rhs) Compares two floats for order.static BigDecimalcreateBigDecimal(String str) Convert aStringto aBigDecimal.static BigIntegercreateBigInteger(String str) Convert aStringto aBigInteger.static DoublecreateDouble(String str) Convert aStringto aDouble.static FloatcreateFloat(String str) Convert aStringto aFloat.static IntegercreateInteger(String str) Convert aStringto aInteger, handling hex and octal notations.static LongcreateLong(String str) Convert aStringto aLong.static NumbercreateNumber(String str) Turns a string value into a java.lang.Number.static booleanChecks whether theStringcontains only digit characters.static booleanChecks whether the String a valid Java number.static bytemax(byte[] array) Returns the maximum value in an array.static bytemax(byte a, byte b, byte c) Gets the maximum of threebytevalues.static doublemax(double[] array) Returns the maximum value in an array.static doublemax(double a, double b, double c) Gets the maximum of threedoublevalues.static floatmax(float[] array) Returns the maximum value in an array.static floatmax(float a, float b, float c) Gets the maximum of threefloatvalues.static intmax(int[] array) Returns the maximum value in an array.static intmax(int a, int b, int c) Gets the maximum of threeintvalues.static longmax(long[] array) Returns the maximum value in an array.static longmax(long a, long b, long c) Gets the maximum of threelongvalues.static shortmax(short[] array) Returns the maximum value in an array.static shortmax(short a, short b, short c) Gets the maximum of threeshortvalues.static bytemin(byte[] array) Returns the minimum value in an array.static bytemin(byte a, byte b, byte c) Gets the minimum of threebytevalues.static doublemin(double[] array) Returns the minimum value in an array.static doublemin(double a, double b, double c) Gets the minimum of threedoublevalues.static floatmin(float[] array) Returns the minimum value in an array.static floatmin(float a, float b, float c) Gets the minimum of threefloatvalues.static intmin(int[] array) Returns the minimum value in an array.static intmin(int a, int b, int c) Gets the minimum of threeintvalues.static longmin(long[] array) Returns the minimum value in an array.static longmin(long a, long b, long c) Gets the minimum of threelongvalues.static shortmin(short[] array) Returns the minimum value in an array.static shortmin(short a, short b, short c) Gets the minimum of threeshortvalues.static intstringToInt(String str) Deprecated.static intstringToInt(String str, int defaultValue) Deprecated.UsetoInt(String, int)This method will be removed in Commons Lang 3.0static byteConvert aStringto abyte, returningzeroif the conversion fails.static byteConvert aStringto abyte, returning a default value if the conversion fails.static doubleConvert aStringto adouble, returning0.0dif the conversion fails.static doubleConvert aStringto adouble, returning a default value if the conversion fails.static floatConvert aStringto afloat, returning0.0fif the conversion fails.static floatConvert aStringto afloat, returning a default value if the conversion fails.static intConvert aStringto anint, returningzeroif the conversion fails.static intConvert aStringto anint, returning a default value if the conversion fails.static longConvert aStringto along, returningzeroif the conversion fails.static longConvert aStringto along, returning a default value if the conversion fails.static shortConvert aStringto ashort, returningzeroif the conversion fails.static shortConvert aStringto anshort, returning a default value if the conversion fails.
-
Field Details
-
LONG_ZERO
Reusable Long constant for zero. -
LONG_ONE
Reusable Long constant for one. -
LONG_MINUS_ONE
Reusable Long constant for minus one. -
INTEGER_ZERO
Reusable Integer constant for zero. -
INTEGER_ONE
Reusable Integer constant for one. -
INTEGER_MINUS_ONE
Reusable Integer constant for minus one. -
SHORT_ZERO
Reusable Short constant for zero. -
SHORT_ONE
Reusable Short constant for one. -
SHORT_MINUS_ONE
Reusable Short constant for minus one. -
BYTE_ZERO
Reusable Byte constant for zero. -
BYTE_ONE
Reusable Byte constant for one. -
BYTE_MINUS_ONE
Reusable Byte constant for minus one. -
DOUBLE_ZERO
Reusable Double constant for zero. -
DOUBLE_ONE
Reusable Double constant for one. -
DOUBLE_MINUS_ONE
Reusable Double constant for minus one. -
FLOAT_ZERO
Reusable Float constant for zero. -
FLOAT_ONE
Reusable Float constant for one. -
FLOAT_MINUS_ONE
Reusable Float constant for minus one.
-
-
Constructor Details
-
NumberUtils
public NumberUtils()NumberUtilsinstances should NOT be constructed in standard programming. Instead, the class should be used asNumberUtils.toInt("6");.This constructor is public to permit tools that require a JavaBean instance to operate.
-
-
Method Details
-
stringToInt
Deprecated.UsetoInt(String)This method will be removed in Commons Lang 3.0Convert a
Stringto anint, returningzeroif the conversion fails.If the string is
null,zerois returned.NumberUtils.stringToInt(null) = 0 NumberUtils.stringToInt("") = 0 NumberUtils.stringToInt("1") = 1- Parameters:
str- the string to convert, may be null- Returns:
- the int represented by the string, or
zeroif conversion fails
-
toInt
Convert a
Stringto anint, returningzeroif the conversion fails.If the string is
null,zerois returned.NumberUtils.toInt(null) = 0 NumberUtils.toInt("") = 0 NumberUtils.toInt("1") = 1- Parameters:
str- the string to convert, may be null- Returns:
- the int represented by the string, or
zeroif conversion fails - Since:
- 2.1
-
stringToInt
Deprecated.UsetoInt(String, int)This method will be removed in Commons Lang 3.0Convert a
Stringto anint, returning a default value if the conversion fails.If the string is
null, the default value is returned.NumberUtils.stringToInt(null, 1) = 1 NumberUtils.stringToInt("", 1) = 1 NumberUtils.stringToInt("1", 0) = 1- Parameters:
str- the string to convert, may be nulldefaultValue- the default value- Returns:
- the int represented by the string, or the default if conversion fails
-
toInt
Convert a
Stringto anint, returning a default value if the conversion fails.If the string is
null, the default value is returned.NumberUtils.toInt(null, 1) = 1 NumberUtils.toInt("", 1) = 1 NumberUtils.toInt("1", 0) = 1- Parameters:
str- the string to convert, may be nulldefaultValue- the default value- Returns:
- the int represented by the string, or the default if conversion fails
- Since:
- 2.1
-
toLong
Convert a
Stringto along, returningzeroif the conversion fails.If the string is
null,zerois returned.NumberUtils.toLong(null) = 0L NumberUtils.toLong("") = 0L NumberUtils.toLong("1") = 1L- Parameters:
str- the string to convert, may be null- Returns:
- the long represented by the string, or
0if conversion fails - Since:
- 2.1
-
toLong
Convert a
Stringto along, returning a default value if the conversion fails.If the string is
null, the default value is returned.NumberUtils.toLong(null, 1L) = 1L NumberUtils.toLong("", 1L) = 1L NumberUtils.toLong("1", 0L) = 1L- Parameters:
str- the string to convert, may be nulldefaultValue- the default value- Returns:
- the long represented by the string, or the default if conversion fails
- Since:
- 2.1
-
toFloat
Convert a
Stringto afloat, returning0.0fif the conversion fails.If the string
strisnull,0.0fis returned.NumberUtils.toFloat(null) = 0.0f NumberUtils.toFloat("") = 0.0f NumberUtils.toFloat("1.5") = 1.5f- Parameters:
str- the string to convert, may benull- Returns:
- the float represented by the string, or
0.0fif conversion fails - Since:
- 2.1
-
toFloat
Convert a
Stringto afloat, returning a default value if the conversion fails.If the string
strisnull, the default value is returned.NumberUtils.toFloat(null, 1.1f) = 1.0f NumberUtils.toFloat("", 1.1f) = 1.1f NumberUtils.toFloat("1.5", 0.0f) = 1.5f- Parameters:
str- the string to convert, may benulldefaultValue- the default value- Returns:
- the float represented by the string, or defaultValue if conversion fails
- Since:
- 2.1
-
toDouble
Convert a
Stringto adouble, returning0.0dif the conversion fails.If the string
strisnull,0.0dis returned.NumberUtils.toDouble(null) = 0.0d NumberUtils.toDouble("") = 0.0d NumberUtils.toDouble("1.5") = 1.5d- Parameters:
str- the string to convert, may benull- Returns:
- the double represented by the string, or
0.0dif conversion fails - Since:
- 2.1
-
toDouble
Convert a
Stringto adouble, returning a default value if the conversion fails.If the string
strisnull, the default value is returned.NumberUtils.toDouble(null, 1.1d) = 1.1d NumberUtils.toDouble("", 1.1d) = 1.1d NumberUtils.toDouble("1.5", 0.0d) = 1.5d- Parameters:
str- the string to convert, may benulldefaultValue- the default value- Returns:
- the double represented by the string, or defaultValue if conversion fails
- Since:
- 2.1
-
toByte
Convert a
Stringto abyte, returningzeroif the conversion fails.If the string is
null,zerois returned.NumberUtils.toByte(null) = 0 NumberUtils.toByte("") = 0 NumberUtils.toByte("1") = 1- Parameters:
str- the string to convert, may be null- Returns:
- the byte represented by the string, or
zeroif conversion fails - Since:
- 2.5
-
toByte
Convert a
Stringto abyte, returning a default value if the conversion fails.If the string is
null, the default value is returned.NumberUtils.toByte(null, 1) = 1 NumberUtils.toByte("", 1) = 1 NumberUtils.toByte("1", 0) = 1- Parameters:
str- the string to convert, may be nulldefaultValue- the default value- Returns:
- the byte represented by the string, or the default if conversion fails
- Since:
- 2.5
-
toShort
Convert a
Stringto ashort, returningzeroif the conversion fails.If the string is
null,zerois returned.NumberUtils.toShort(null) = 0 NumberUtils.toShort("") = 0 NumberUtils.toShort("1") = 1- Parameters:
str- the string to convert, may be null- Returns:
- the short represented by the string, or
zeroif conversion fails - Since:
- 2.5
-
toShort
Convert a
Stringto anshort, returning a default value if the conversion fails.If the string is
null, the default value is returned.NumberUtils.toShort(null, 1) = 1 NumberUtils.toShort("", 1) = 1 NumberUtils.toShort("1", 0) = 1- Parameters:
str- the string to convert, may be nulldefaultValue- the default value- Returns:
- the short represented by the string, or the default if conversion fails
- Since:
- 2.5
-
createNumber
Turns a string value into a java.lang.Number.
First, the value is examined for a type qualifier on the end (
'f','F','d','D','l','L'). If it is found, it starts trying to create successively larger types from the type specified until one is found that can represent the value.If a type specifier is not found, it will check for a decimal point and then try successively larger types from
IntegertoBigIntegerand fromFloattoBigDecimal.If the string starts with
0xor-0x, it will be interpreted as a hexadecimal integer. Values with leading0's will not be interpreted as octal.Returns
nullif the string isnull.This method does not trim the input string, i.e., strings with leading or trailing spaces will generate NumberFormatExceptions.
- Parameters:
str- String containing a number, may be null- Returns:
- Number created from the string
- Throws:
NumberFormatException- if the value cannot be converted
-
createFloat
Convert a
Stringto aFloat.Returns
nullif the string isnull.- Parameters:
str- aStringto convert, may be null- Returns:
- converted
Float - Throws:
NumberFormatException- if the value cannot be converted
-
createDouble
Convert a
Stringto aDouble.Returns
nullif the string isnull.- Parameters:
str- aStringto convert, may be null- Returns:
- converted
Double - Throws:
NumberFormatException- if the value cannot be converted
-
createInteger
Convert a
Stringto aInteger, handling hex and octal notations.Returns
nullif the string isnull.- Parameters:
str- aStringto convert, may be null- Returns:
- converted
Integer - Throws:
NumberFormatException- if the value cannot be converted
-
createLong
Convert a
Stringto aLong.Returns
nullif the string isnull.- Parameters:
str- aStringto convert, may be null- Returns:
- converted
Long - Throws:
NumberFormatException- if the value cannot be converted
-
createBigInteger
Convert a
Stringto aBigInteger.Returns
nullif the string isnull.- Parameters:
str- aStringto convert, may be null- Returns:
- converted
BigInteger - Throws:
NumberFormatException- if the value cannot be converted
-
createBigDecimal
Convert a
Stringto aBigDecimal.Returns
nullif the string isnull.- Parameters:
str- aStringto convert, may be null- Returns:
- converted
BigDecimal - Throws:
NumberFormatException- if the value cannot be converted
-
min
public static long min(long[] array) Returns the minimum value in an array.
- Parameters:
array- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
IllegalArgumentException- ifarrayisnullIllegalArgumentException- ifarrayis empty
-
min
public static int min(int[] array) Returns the minimum value in an array.
- Parameters:
array- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
IllegalArgumentException- ifarrayisnullIllegalArgumentException- ifarrayis empty
-
min
public static short min(short[] array) Returns the minimum value in an array.
- Parameters:
array- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
IllegalArgumentException- ifarrayisnullIllegalArgumentException- ifarrayis empty
-
min
public static byte min(byte[] array) Returns the minimum value in an array.
- Parameters:
array- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
IllegalArgumentException- ifarrayisnullIllegalArgumentException- ifarrayis empty
-
min
public static double min(double[] array) Returns the minimum value in an array.
- Parameters:
array- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
IllegalArgumentException- ifarrayisnullIllegalArgumentException- ifarrayis empty- See Also:
-
min
public static float min(float[] array) Returns the minimum value in an array.
- Parameters:
array- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
IllegalArgumentException- ifarrayisnullIllegalArgumentException- ifarrayis empty- See Also:
-
max
public static long max(long[] array) Returns the maximum value in an array.
- Parameters:
array- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
IllegalArgumentException- ifarrayisnullIllegalArgumentException- ifarrayis empty
-
max
public static int max(int[] array) Returns the maximum value in an array.
- Parameters:
array- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
IllegalArgumentException- ifarrayisnullIllegalArgumentException- ifarrayis empty
-
max
public static short max(short[] array) Returns the maximum value in an array.
- Parameters:
array- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
IllegalArgumentException- ifarrayisnullIllegalArgumentException- ifarrayis empty
-
max
public static byte max(byte[] array) Returns the maximum value in an array.
- Parameters:
array- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
IllegalArgumentException- ifarrayisnullIllegalArgumentException- ifarrayis empty
-
max
public static double max(double[] array) Returns the maximum value in an array.
- Parameters:
array- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
IllegalArgumentException- ifarrayisnullIllegalArgumentException- ifarrayis empty- See Also:
-
max
public static float max(float[] array) Returns the maximum value in an array.
- Parameters:
array- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
IllegalArgumentException- ifarrayisnullIllegalArgumentException- ifarrayis empty- See Also:
-
min
public static long min(long a, long b, long c) Gets the minimum of three
longvalues.- Parameters:
a- value 1b- value 2c- value 3- Returns:
- the smallest of the values
-
min
public static int min(int a, int b, int c) Gets the minimum of three
intvalues.- Parameters:
a- value 1b- value 2c- value 3- Returns:
- the smallest of the values
-
min
public static short min(short a, short b, short c) Gets the minimum of three
shortvalues.- Parameters:
a- value 1b- value 2c- value 3- Returns:
- the smallest of the values
-
min
public static byte min(byte a, byte b, byte c) Gets the minimum of three
bytevalues.- Parameters:
a- value 1b- value 2c- value 3- Returns:
- the smallest of the values
-
min
public static double min(double a, double b, double c) Gets the minimum of three
doublevalues.If any value is
NaN,NaNis returned. Infinity is handled.- Parameters:
a- value 1b- value 2c- value 3- Returns:
- the smallest of the values
- See Also:
-
min
public static float min(float a, float b, float c) Gets the minimum of three
floatvalues.If any value is
NaN,NaNis returned. Infinity is handled.- Parameters:
a- value 1b- value 2c- value 3- Returns:
- the smallest of the values
- See Also:
-
max
public static long max(long a, long b, long c) Gets the maximum of three
longvalues.- Parameters:
a- value 1b- value 2c- value 3- Returns:
- the largest of the values
-
max
public static int max(int a, int b, int c) Gets the maximum of three
intvalues.- Parameters:
a- value 1b- value 2c- value 3- Returns:
- the largest of the values
-
max
public static short max(short a, short b, short c) Gets the maximum of three
shortvalues.- Parameters:
a- value 1b- value 2c- value 3- Returns:
- the largest of the values
-
max
public static byte max(byte a, byte b, byte c) Gets the maximum of three
bytevalues.- Parameters:
a- value 1b- value 2c- value 3- Returns:
- the largest of the values
-
max
public static double max(double a, double b, double c) Gets the maximum of three
doublevalues.If any value is
NaN,NaNis returned. Infinity is handled.- Parameters:
a- value 1b- value 2c- value 3- Returns:
- the largest of the values
- See Also:
-
max
public static float max(float a, float b, float c) Gets the maximum of three
floatvalues.If any value is
NaN,NaNis returned. Infinity is handled.- Parameters:
a- value 1b- value 2c- value 3- Returns:
- the largest of the values
- See Also:
-
compare
public static int compare(double lhs, double rhs) Compares two
doublesfor order.This method is more comprehensive than the standard Java greater than, less than and equals operators.
- It returns
-1if the first value is less than the second. - It returns
+1if the first value is greater than the second. - It returns
0if the values are equal.
The ordering is as follows, largest to smallest:
- NaN
- Positive infinity
- Maximum double
- Normal positive numbers
- +0.0
- -0.0
- Normal negative numbers
- Minimum double (
-Double.MAX_VALUE) - Negative infinity
Comparing
NaNwithNaNwill return0.- Parameters:
lhs- the firstdoublerhs- the seconddouble- Returns:
-1if lhs is less,+1if greater,0if equal to rhs
- It returns
-
compare
public static int compare(float lhs, float rhs) Compares two floats for order.
This method is more comprehensive than the standard Java greater than, less than and equals operators.
- It returns
-1if the first value is less than the second. - It returns
+1if the first value is greater than the second. - It returns
0if the values are equal.
The ordering is as follows, largest to smallest:
- NaN
- Positive infinity
- Maximum float
- Normal positive numbers
- +0.0
- -0.0
- Normal negative numbers
- Minimum float (
-Float.MAX_VALUE) - Negative infinity
Comparing
NaNwithNaNwill return0.- Parameters:
lhs- the firstfloatrhs- the secondfloat- Returns:
-1if lhs is less,+1if greater,0if equal to rhs
- It returns
-
isDigits
Checks whether the
Stringcontains only digit characters.Nulland empty String will returnfalse.- Parameters:
str- theStringto check- Returns:
trueif str contains only unicode numeric
-
isNumber
Checks whether the String a valid Java number.
Valid numbers include hexadecimal marked with the
0xqualifier, scientific notation and numbers marked with a type qualifier (e.g. 123L).Nulland empty String will returnfalse.- Parameters:
str- theStringto check- Returns:
trueif the string is a correctly formatted number
-
toInt(String)This method will be removed in Commons Lang 3.0