|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.lang.math.NumberUtils
Provides extra functionality for Java Number classes.
Field Summary | |
static java.lang.Byte |
BYTE_MINUS_ONE
Reusable Byte constant for minus one. |
static java.lang.Byte |
BYTE_ONE
Reusable Byte constant for one. |
static java.lang.Byte |
BYTE_ZERO
Reusable Byte constant for zero. |
static java.lang.Double |
DOUBLE_MINUS_ONE
Reusable Double constant for minus one. |
static java.lang.Double |
DOUBLE_ONE
Reusable Double constant for one. |
static java.lang.Double |
DOUBLE_ZERO
Reusable Double constant for zero. |
static java.lang.Float |
FLOAT_MINUS_ONE
Reusable Float constant for minus one. |
static java.lang.Float |
FLOAT_ONE
Reusable Float constant for one. |
static java.lang.Float |
FLOAT_ZERO
Reusable Float constant for zero. |
static java.lang.Integer |
INTEGER_MINUS_ONE
Reusable Integer constant for minus one. |
static java.lang.Integer |
INTEGER_ONE
Reusable Integer constant for one. |
static java.lang.Integer |
INTEGER_ZERO
Reusable Integer constant for zero. |
static java.lang.Long |
LONG_MINUS_ONE
Reusable Long constant for minus one. |
static java.lang.Long |
LONG_ONE
Reusable Long constant for one. |
static java.lang.Long |
LONG_ZERO
Reusable Long constant for zero. |
static java.lang.Short |
SHORT_MINUS_ONE
Reusable Short constant for minus one. |
static java.lang.Short |
SHORT_ONE
Reusable Short constant for one. |
static java.lang.Short |
SHORT_ZERO
Reusable Short constant for zero. |
Constructor Summary | |
NumberUtils()
NumberUtils instances should NOT be constructed in standard programming. |
Method Summary | |
static int |
compare(double lhs,
double rhs)
Compares two doubles for order. |
static int |
compare(float lhs,
float rhs)
Compares two floats for order. |
static java.math.BigDecimal |
createBigDecimal(java.lang.String str)
Convert a String to a BigDecimal . |
static java.math.BigInteger |
createBigInteger(java.lang.String str)
Convert a String to a BigInteger . |
static java.lang.Double |
createDouble(java.lang.String str)
Convert a String to a Double . |
static java.lang.Float |
createFloat(java.lang.String str)
Convert a String to a Float . |
static java.lang.Integer |
createInteger(java.lang.String str)
Convert a String to a Integer , handling
hex and octal notations. |
static java.lang.Long |
createLong(java.lang.String str)
Convert a String to a Long . |
static java.lang.Number |
createNumber(java.lang.String str)
Turns a string value into a java.lang.Number. |
static boolean |
isDigits(java.lang.String str)
Checks whether the String contains only
digit characters. |
static boolean |
isNumber(java.lang.String str)
Checks whether the String a valid Java number. |
static byte |
max(byte[] array)
Returns the maximum value in an array. |
static byte |
max(byte a,
byte b,
byte c)
Gets the maximum of three byte values. |
static double |
max(double[] array)
Returns the maximum value in an array. |
static double |
max(double a,
double b,
double c)
Gets the maximum of three double values. |
static float |
max(float[] array)
Returns the maximum value in an array. |
static float |
max(float a,
float b,
float c)
Gets the maximum of three float values. |
static int |
max(int[] array)
Returns the maximum value in an array. |
static int |
max(int a,
int b,
int c)
Gets the maximum of three int values. |
static long |
max(long[] array)
Returns the maximum value in an array. |
static long |
max(long a,
long b,
long c)
Gets the maximum of three long values. |
static short |
max(short[] array)
Returns the maximum value in an array. |
static short |
max(short a,
short b,
short c)
Gets the maximum of three short values. |
static byte |
min(byte[] array)
Returns the minimum value in an array. |
static byte |
min(byte a,
byte b,
byte c)
Gets the minimum of three byte values. |
static double |
min(double[] array)
Returns the minimum value in an array. |
static double |
min(double a,
double b,
double c)
Gets the minimum of three double values. |
static float |
min(float[] array)
Returns the minimum value in an array. |
static float |
min(float a,
float b,
float c)
Gets the minimum of three float values. |
static int |
min(int[] array)
Returns the minimum value in an array. |
static int |
min(int a,
int b,
int c)
Gets the minimum of three int values. |
static long |
min(long[] array)
Returns the minimum value in an array. |
static long |
min(long a,
long b,
long c)
Gets the minimum of three long values. |
static short |
min(short[] array)
Returns the minimum value in an array. |
static short |
min(short a,
short b,
short c)
Gets the minimum of three short values. |
static int |
stringToInt(java.lang.String str)
Deprecated. Use toInt(String)
This method will be removed in Commons Lang 3.0 |
static int |
stringToInt(java.lang.String str,
int defaultValue)
Deprecated. Use toInt(String, int)
This method will be removed in Commons Lang 3.0 |
static double |
toDouble(java.lang.String str)
Convert a String to a double , returning
0.0d if the conversion fails. |
static double |
toDouble(java.lang.String str,
double defaultValue)
Convert a String to a double , returning a
default value if the conversion fails. |
static float |
toFloat(java.lang.String str)
Convert a String to a float , returning
0.0f if the conversion fails. |
static float |
toFloat(java.lang.String str,
float defaultValue)
Convert a String to a float , returning a
default value if the conversion fails. |
static int |
toInt(java.lang.String str)
Convert a String to an int , returning
zero if the conversion fails. |
static int |
toInt(java.lang.String str,
int defaultValue)
Convert a String to an int , returning a
default value if the conversion fails. |
static long |
toLong(java.lang.String str)
Convert a String to a long , returning
zero if the conversion fails. |
static long |
toLong(java.lang.String str,
long defaultValue)
Convert a String to a long , returning a
default value if the conversion fails. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final java.lang.Long LONG_ZERO
public static final java.lang.Long LONG_ONE
public static final java.lang.Long LONG_MINUS_ONE
public static final java.lang.Integer INTEGER_ZERO
public static final java.lang.Integer INTEGER_ONE
public static final java.lang.Integer INTEGER_MINUS_ONE
public static final java.lang.Short SHORT_ZERO
public static final java.lang.Short SHORT_ONE
public static final java.lang.Short SHORT_MINUS_ONE
public static final java.lang.Byte BYTE_ZERO
public static final java.lang.Byte BYTE_ONE
public static final java.lang.Byte BYTE_MINUS_ONE
public static final java.lang.Double DOUBLE_ZERO
public static final java.lang.Double DOUBLE_ONE
public static final java.lang.Double DOUBLE_MINUS_ONE
public static final java.lang.Float FLOAT_ZERO
public static final java.lang.Float FLOAT_ONE
public static final java.lang.Float FLOAT_MINUS_ONE
Constructor Detail |
public NumberUtils()
NumberUtils
instances should NOT be constructed in standard programming.
Instead, the class should be used as NumberUtils.stringToInt("6");
.
This constructor is public to permit tools that require a JavaBean instance to operate.
Method Detail |
public static int stringToInt(java.lang.String str)
toInt(String)
This method will be removed in Commons Lang 3.0
Convert a String
to an int
, returning
zero
if the conversion fails.
If the string is null
, zero
is returned.
NumberUtils.stringToInt(null) = 0 NumberUtils.stringToInt("") = 0 NumberUtils.stringToInt("1") = 1
str
- the string to convert, may be null
zero
if
conversion failspublic static int toInt(java.lang.String str)
Convert a String
to an int
, returning
zero
if the conversion fails.
If the string is null
, zero
is returned.
NumberUtils.toInt(null) = 0 NumberUtils.toInt("") = 0 NumberUtils.toInt("1") = 1
str
- the string to convert, may be null
zero
if
conversion failspublic static int stringToInt(java.lang.String str, int defaultValue)
toInt(String, int)
This method will be removed in Commons Lang 3.0
Convert a String
to an int
, 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
str
- the string to convert, may be nulldefaultValue
- the default value
public static int toInt(java.lang.String str, int defaultValue)
Convert a String
to an int
, 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
str
- the string to convert, may be nulldefaultValue
- the default value
public static long toLong(java.lang.String str)
Convert a String
to a long
, returning
zero
if the conversion fails.
If the string is null
, zero
is returned.
NumberUtils.toLong(null) = 0L NumberUtils.toLong("") = 0L NumberUtils.toLong("1") = 1L
str
- the string to convert, may be null
0
if
conversion failspublic static long toLong(java.lang.String str, long defaultValue)
Convert a String
to a long
, 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
str
- the string to convert, may be nulldefaultValue
- the default value
public static float toFloat(java.lang.String str)
Convert a String
to a float
, returning
0.0f
if the conversion fails.
If the string str
is null
,
0.0f
is returned.
NumberUtils.toFloat(null) = 0.0f NumberUtils.toFloat("") = 0.0f NumberUtils.toFloat("1.5") = 1.5f
str
- the string to convert, may be null
0.0f
if conversion failspublic static float toFloat(java.lang.String str, float defaultValue)
Convert a String
to a float
, returning a
default value if the conversion fails.
If the string str
is null
, 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
str
- the string to convert, may be null
defaultValue
- the default value
public static double toDouble(java.lang.String str)
Convert a String
to a double
, returning
0.0d
if the conversion fails.
If the string str
is null
,
0.0d
is returned.
NumberUtils.toDouble(null) = 0.0d NumberUtils.toDouble("") = 0.0d NumberUtils.toDouble("1.5") = 1.5d
str
- the string to convert, may be null
0.0d
if conversion failspublic static double toDouble(java.lang.String str, double defaultValue)
Convert a String
to a double
, returning a
default value if the conversion fails.
If the string str
is null
, 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
str
- the string to convert, may be null
defaultValue
- the default value
public static java.lang.Number createNumber(java.lang.String str) throws java.lang.NumberFormatException
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 Integer
to
BigInteger
and from Float
to
BigDecimal
.
If the string starts with 0x
or -0x
, it
will be interpreted as a hexadecimal integer. Values with leading
0
's will not be interpreted as octal.
Returns null
if the string is null
.
This method does not trim the input string, i.e., strings with leading or trailing spaces will generate NumberFormatExceptions.
str
- String containing a number, may be null
java.lang.NumberFormatException
- if the value cannot be convertedpublic static java.lang.Float createFloat(java.lang.String str)
Convert a String
to a Float
.
Returns null
if the string is null
.
str
- a String
to convert, may be null
Float
java.lang.NumberFormatException
- if the value cannot be convertedpublic static java.lang.Double createDouble(java.lang.String str)
Convert a String
to a Double
.
Returns null
if the string is null
.
str
- a String
to convert, may be null
Double
java.lang.NumberFormatException
- if the value cannot be convertedpublic static java.lang.Integer createInteger(java.lang.String str)
Convert a String
to a Integer
, handling
hex and octal notations.
Returns null
if the string is null
.
str
- a String
to convert, may be null
Integer
java.lang.NumberFormatException
- if the value cannot be convertedpublic static java.lang.Long createLong(java.lang.String str)
Convert a String
to a Long
.
Returns null
if the string is null
.
str
- a String
to convert, may be null
Long
java.lang.NumberFormatException
- if the value cannot be convertedpublic static java.math.BigInteger createBigInteger(java.lang.String str)
Convert a String
to a BigInteger
.
Returns null
if the string is null
.
str
- a String
to convert, may be null
BigInteger
java.lang.NumberFormatException
- if the value cannot be convertedpublic static java.math.BigDecimal createBigDecimal(java.lang.String str)
Convert a String
to a BigDecimal
.
Returns null
if the string is null
.
str
- a String
to convert, may be null
BigDecimal
java.lang.NumberFormatException
- if the value cannot be convertedpublic static long min(long[] array)
Returns the minimum value in an array.
array
- an array, must not be null or empty
java.lang.IllegalArgumentException
- if array
is null
java.lang.IllegalArgumentException
- if array
is emptypublic static int min(int[] array)
Returns the minimum value in an array.
array
- an array, must not be null or empty
java.lang.IllegalArgumentException
- if array
is null
java.lang.IllegalArgumentException
- if array
is emptypublic static short min(short[] array)
Returns the minimum value in an array.
array
- an array, must not be null or empty
java.lang.IllegalArgumentException
- if array
is null
java.lang.IllegalArgumentException
- if array
is emptypublic static byte min(byte[] array)
Returns the minimum value in an array.
array
- an array, must not be null or empty
java.lang.IllegalArgumentException
- if array
is null
java.lang.IllegalArgumentException
- if array
is emptypublic static double min(double[] array)
Returns the minimum value in an array.
array
- an array, must not be null or empty
java.lang.IllegalArgumentException
- if array
is null
java.lang.IllegalArgumentException
- if array
is emptyIEEE754rUtils for a version of this method that handles NaN differently
public static float min(float[] array)
Returns the minimum value in an array.
array
- an array, must not be null or empty
java.lang.IllegalArgumentException
- if array
is null
java.lang.IllegalArgumentException
- if array
is emptyIEEE754rUtils for a version of this method that handles NaN differently
public static long max(long[] array)
Returns the maximum value in an array.
array
- an array, must not be null or empty
java.lang.IllegalArgumentException
- if array
is null
java.lang.IllegalArgumentException
- if array
is emptypublic static int max(int[] array)
Returns the maximum value in an array.
array
- an array, must not be null or empty
java.lang.IllegalArgumentException
- if array
is null
java.lang.IllegalArgumentException
- if array
is emptypublic static short max(short[] array)
Returns the maximum value in an array.
array
- an array, must not be null or empty
java.lang.IllegalArgumentException
- if array
is null
java.lang.IllegalArgumentException
- if array
is emptypublic static byte max(byte[] array)
Returns the maximum value in an array.
array
- an array, must not be null or empty
java.lang.IllegalArgumentException
- if array
is null
java.lang.IllegalArgumentException
- if array
is emptypublic static double max(double[] array)
Returns the maximum value in an array.
array
- an array, must not be null or empty
java.lang.IllegalArgumentException
- if array
is null
java.lang.IllegalArgumentException
- if array
is emptyIEEE754rUtils for a version of this method that handles NaN differently
public static float max(float[] array)
Returns the maximum value in an array.
array
- an array, must not be null or empty
java.lang.IllegalArgumentException
- if array
is null
java.lang.IllegalArgumentException
- if array
is emptyIEEE754rUtils for a version of this method that handles NaN differently
public static long min(long a, long b, long c)
Gets the minimum of three long
values.
a
- value 1b
- value 2c
- value 3
public static int min(int a, int b, int c)
Gets the minimum of three int
values.
a
- value 1b
- value 2c
- value 3
public static short min(short a, short b, short c)
Gets the minimum of three short
values.
a
- value 1b
- value 2c
- value 3
public static byte min(byte a, byte b, byte c)
Gets the minimum of three byte
values.
a
- value 1b
- value 2c
- value 3
public static double min(double a, double b, double c)
Gets the minimum of three double
values.
If any value is NaN
, NaN
is
returned. Infinity is handled.
a
- value 1b
- value 2c
- value 3
for a version of this method that handles NaN differently
public static float min(float a, float b, float c)
Gets the minimum of three float
values.
If any value is NaN
, NaN
is
returned. Infinity is handled.
a
- value 1b
- value 2c
- value 3
for a version of this method that handles NaN differently
public static long max(long a, long b, long c)
Gets the maximum of three long
values.
a
- value 1b
- value 2c
- value 3
public static int max(int a, int b, int c)
Gets the maximum of three int
values.
a
- value 1b
- value 2c
- value 3
public static short max(short a, short b, short c)
Gets the maximum of three short
values.
a
- value 1b
- value 2c
- value 3
public static byte max(byte a, byte b, byte c)
Gets the maximum of three byte
values.
a
- value 1b
- value 2c
- value 3
public static double max(double a, double b, double c)
Gets the maximum of three double
values.
If any value is NaN
, NaN
is
returned. Infinity is handled.
a
- value 1b
- value 2c
- value 3
for a version of this method that handles NaN differently
public static float max(float a, float b, float c)
Gets the maximum of three float
values.
If any value is NaN
, NaN
is
returned. Infinity is handled.
a
- value 1b
- value 2c
- value 3
for a version of this method that handles NaN differently
public static int compare(double lhs, double rhs)
Compares two doubles
for order.
This method is more comprehensive than the standard Java greater than, less than and equals operators.
-1
if the first value is less than the second.+1
if the first value is greater than the second.0
if the values are equal.The ordering is as follows, largest to smallest:
-Double.MAX_VALUE
)
Comparing NaN
with NaN
will
return 0
.
lhs
- the first double
rhs
- the second double
-1
if lhs is less, +1
if greater,
0
if equal to rhspublic 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.
-1
if the first value is less than the second.
+1
if the first value is greater than the second.
0
if the values are equal.
The ordering is as follows, largest to smallest:
-Float.MAX_VALUE
)
Comparing NaN
with NaN
will return
0
.
lhs
- the first float
rhs
- the second float
-1
if lhs is less, +1
if greater,
0
if equal to rhspublic static boolean isDigits(java.lang.String str)
Checks whether the String
contains only
digit characters.
Null
and empty String will return
false
.
str
- the String
to check
true
if str contains only unicode numericpublic static boolean isNumber(java.lang.String str)
Checks whether the String a valid Java number.
Valid numbers include hexadecimal marked with the 0x
qualifier, scientific notation and numbers marked with a type
qualifier (e.g. 123L).
Null
and empty String will return
false
.
str
- the String
to check
true
if the string is a correctly formatted number
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |