| java.lang.Object org.apache.commons.lang.math.NumberUtils
NumberUtils | public class NumberUtils (Code) | | Provides extra functionality for Java Number classes.
author: Rand McNeely author: Stephen Colebourne author: Steve Downey author: Eric Pugh author: Phil Steitz author: Matthew Hawthorne author: Gary Gregory author: Fredrik Westermarck since: 2.0 version: $Id: NumberUtils.java 491076 2006-12-29 18:48:37Z bayard $ |
Constructor Summary | |
public | NumberUtils() NumberUtils instances should NOT be constructed in standard programming.
|
Method Summary | |
public static int | compare(double lhs, double rhs) | 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
-1 if the first value is less than the second. | public static BigDecimal | createBigDecimal(String str) | public static BigInteger | createBigInteger(String str) | public static Double | createDouble(String str) | public static Float | createFloat(String str) | public static Integer | createInteger(String str) | public static Long | createLong(String str) | public static Number | createNumber(String str) 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' ). | public static boolean | isDigits(String str) | public static boolean | isNumber(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. | public static long | max(long[] array) | public static int | max(int[] array) | public static short | max(short[] array) | public static byte | max(byte[] array) | public static double | max(double[] array) | public static float | max(float[] array) | public static long | max(long a, long b, long c) | public static int | max(int a, int b, int c) | public static short | max(short a, short b, short c) | public static byte | max(byte a, byte b, byte c) | 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. | 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. | public static long | min(long[] array) | public static int | min(int[] array) | public static short | min(short[] array) | public static byte | min(byte[] array) | public static double | min(double[] array) | public static float | min(float[] array) | public static long | min(long a, long b, long c) | public static int | min(int a, int b, int c) | public static short | min(short a, short b, short c) | public static byte | min(byte a, byte b, byte c) | 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. | 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. | public static int | stringToInt(String str) | public static int | stringToInt(String str, int defaultValue) | public static double | toDouble(String str) | public static double | toDouble(String str, double defaultValue) | public static float | toFloat(String str) | public static float | toFloat(String str, float defaultValue) | public static int | toInt(String str) | public static int | toInt(String str, int defaultValue) | public static long | toLong(String str) | public static long | toLong(String str, long defaultValue) |
BYTE_MINUS_ONE | final public static Byte BYTE_MINUS_ONE(Code) | | Reusable Byte constant for minus one.
|
BYTE_ONE | final public static Byte BYTE_ONE(Code) | | Reusable Byte constant for one.
|
BYTE_ZERO | final public static Byte BYTE_ZERO(Code) | | Reusable Byte constant for zero.
|
DOUBLE_MINUS_ONE | final public static Double DOUBLE_MINUS_ONE(Code) | | Reusable Double constant for minus one.
|
DOUBLE_ONE | final public static Double DOUBLE_ONE(Code) | | Reusable Double constant for one.
|
DOUBLE_ZERO | final public static Double DOUBLE_ZERO(Code) | | Reusable Double constant for zero.
|
FLOAT_MINUS_ONE | final public static Float FLOAT_MINUS_ONE(Code) | | Reusable Float constant for minus one.
|
FLOAT_ONE | final public static Float FLOAT_ONE(Code) | | Reusable Float constant for one.
|
FLOAT_ZERO | final public static Float FLOAT_ZERO(Code) | | Reusable Float constant for zero.
|
INTEGER_MINUS_ONE | final public static Integer INTEGER_MINUS_ONE(Code) | | Reusable Integer constant for minus one.
|
INTEGER_ONE | final public static Integer INTEGER_ONE(Code) | | Reusable Integer constant for one.
|
INTEGER_ZERO | final public static Integer INTEGER_ZERO(Code) | | Reusable Integer constant for zero.
|
LONG_MINUS_ONE | final public static Long LONG_MINUS_ONE(Code) | | Reusable Long constant for minus one.
|
LONG_ONE | final public static Long LONG_ONE(Code) | | Reusable Long constant for one.
|
LONG_ZERO | final public static Long LONG_ZERO(Code) | | Reusable Long constant for zero.
|
SHORT_MINUS_ONE | final public static Short SHORT_MINUS_ONE(Code) | | Reusable Short constant for minus one.
|
SHORT_ONE | final public static Short SHORT_ONE(Code) | | Reusable Short constant for one.
|
SHORT_ZERO | final public static Short SHORT_ZERO(Code) | | Reusable Short constant for zero.
|
NumberUtils | public NumberUtils()(Code) | | 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.
|
compare | public static int compare(double lhs, double rhs)(Code) | | Compares two doubles for order.
This method is more comprehensive than the standard Java greater
than, less than and equals operators.
- It returns
-1 if the first value is less than the second.
- It returns
+1 if the first value is greater than the second.
- It returns
0 if 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 NaN with NaN will
return 0 .
Parameters: lhs - the first double Parameters: rhs - the second double -1 if lhs is less, +1 if greater,0 if equal to rhs |
compare | public static int compare(float lhs, float rhs)(Code) | | Compares two floats for order.
This method is more comprehensive than the standard Java greater than,
less than and equals operators.
- It returns
-1 if the first value is less than the second.
- It returns
+1 if the first value is greater than the second.
- It returns
0 if 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 NaN with NaN will return
0 .
Parameters: lhs - the first float Parameters: rhs - the second float -1 if lhs is less, +1 if greater,0 if equal to rhs |
createBigDecimal | public static BigDecimal createBigDecimal(String str)(Code) | | Convert a String to a BigDecimal .
Returns null if the string is null .
Parameters: str - a String to convert, may be null converted BigDecimal throws: NumberFormatException - if the value cannot be converted |
createBigInteger | public static BigInteger createBigInteger(String str)(Code) | | Convert a String to a BigInteger .
Returns null if the string is null .
Parameters: str - a String to convert, may be null converted BigInteger throws: NumberFormatException - if the value cannot be converted |
createDouble | public static Double createDouble(String str)(Code) | | Convert a String to a Double .
Returns null if the string is null .
Parameters: str - a String to convert, may be null converted Double throws: NumberFormatException - if the value cannot be converted |
createFloat | public static Float createFloat(String str)(Code) | | Convert a String to a Float .
Returns null if the string is null .
Parameters: str - a String to convert, may be null converted Float throws: NumberFormatException - if the value cannot be converted |
createInteger | public static Integer createInteger(String str)(Code) | | Convert a String to a Integer , handling
hex and octal notations.
Returns null if the string is null .
Parameters: str - a String to convert, may be null converted Integer throws: NumberFormatException - if the value cannot be converted |
createLong | public static Long createLong(String str)(Code) | | Convert a String to a Long .
Returns null if the string is null .
Parameters: str - a String to convert, may be null converted Long throws: NumberFormatException - if the value cannot be converted |
createNumber | public static Number createNumber(String str) throws NumberFormatException(Code) | | 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.
Parameters: str - String containing a number, may be null Number created from the string throws: NumberFormatException - if the value cannot be converted |
isDigits | public static boolean isDigits(String str)(Code) | | Checks whether the String contains only
digit characters.
Null and empty String will return
false .
Parameters: str - the String to check true if str contains only unicode numeric |
isNumber | public static boolean isNumber(String str)(Code) | | 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 .
Parameters: str - the String to check true if the string is a correctly formatted number |
max | public static long max(long[] array)(Code) | | Returns the maximum value in an array.
Parameters: array - an array, must not be null or empty the minimum value in the array throws: IllegalArgumentException - if array is null throws: IllegalArgumentException - if array is empty |
max | public static int max(int[] array)(Code) | | Returns the maximum value in an array.
Parameters: array - an array, must not be null or empty the minimum value in the array throws: IllegalArgumentException - if array is null throws: IllegalArgumentException - if array is empty |
max | public static short max(short[] array)(Code) | | Returns the maximum value in an array.
Parameters: array - an array, must not be null or empty the minimum value in the array throws: IllegalArgumentException - if array is null throws: IllegalArgumentException - if array is empty |
max | public static byte max(byte[] array)(Code) | | Returns the maximum value in an array.
Parameters: array - an array, must not be null or empty the minimum value in the array throws: IllegalArgumentException - if array is null throws: IllegalArgumentException - if array is empty |
max | public static double max(double[] array)(Code) | | Returns the maximum value in an array.
Parameters: array - an array, must not be null or empty the minimum value in the array throws: IllegalArgumentException - if array is null throws: IllegalArgumentException - if array is empty |
max | public static float max(float[] array)(Code) | | Returns the maximum value in an array.
Parameters: array - an array, must not be null or empty the minimum value in the array throws: IllegalArgumentException - if array is null throws: IllegalArgumentException - if array is empty |
max | public static long max(long a, long b, long c)(Code) | | Gets the maximum of three long values.
Parameters: a - value 1 Parameters: b - value 2 Parameters: c - value 3 the largest of the values |
max | public static int max(int a, int b, int c)(Code) | | Gets the maximum of three int values.
Parameters: a - value 1 Parameters: b - value 2 Parameters: c - value 3 the largest of the values |
max | public static short max(short a, short b, short c)(Code) | | Gets the maximum of three short values.
Parameters: a - value 1 Parameters: b - value 2 Parameters: c - value 3 the largest of the values |
max | public static byte max(byte a, byte b, byte c)(Code) | | Gets the maximum of three byte values.
Parameters: a - value 1 Parameters: b - value 2 Parameters: c - value 3 the largest of the values |
max | public static double max(double a, double b, double c)(Code) | | Gets the maximum of three double values.
If any value is NaN , NaN is
returned. Infinity is handled.
Parameters: a - value 1 Parameters: b - value 2 Parameters: c - value 3 the largest of the values |
max | public static float max(float a, float b, float c)(Code) | | Gets the maximum of three float values.
If any value is NaN , NaN is
returned. Infinity is handled.
Parameters: a - value 1 Parameters: b - value 2 Parameters: c - value 3 the largest of the values |
min | public static long min(long[] array)(Code) | | Returns the minimum value in an array.
Parameters: array - an array, must not be null or empty the minimum value in the array throws: IllegalArgumentException - if array is null throws: IllegalArgumentException - if array is empty |
min | public static int min(int[] array)(Code) | | Returns the minimum value in an array.
Parameters: array - an array, must not be null or empty the minimum value in the array throws: IllegalArgumentException - if array is null throws: IllegalArgumentException - if array is empty |
min | public static short min(short[] array)(Code) | | Returns the minimum value in an array.
Parameters: array - an array, must not be null or empty the minimum value in the array throws: IllegalArgumentException - if array is null throws: IllegalArgumentException - if array is empty |
min | public static byte min(byte[] array)(Code) | | Returns the minimum value in an array.
Parameters: array - an array, must not be null or empty the minimum value in the array throws: IllegalArgumentException - if array is null throws: IllegalArgumentException - if array is empty |
min | public static double min(double[] array)(Code) | | Returns the minimum value in an array.
Parameters: array - an array, must not be null or empty the minimum value in the array throws: IllegalArgumentException - if array is null throws: IllegalArgumentException - if array is empty |
min | public static float min(float[] array)(Code) | | Returns the minimum value in an array.
Parameters: array - an array, must not be null or empty the minimum value in the array throws: IllegalArgumentException - if array is null throws: IllegalArgumentException - if array is empty |
min | public static long min(long a, long b, long c)(Code) | | Gets the minimum of three long values.
Parameters: a - value 1 Parameters: b - value 2 Parameters: c - value 3 the smallest of the values |
min | public static int min(int a, int b, int c)(Code) | | Gets the minimum of three int values.
Parameters: a - value 1 Parameters: b - value 2 Parameters: c - value 3 the smallest of the values |
min | public static short min(short a, short b, short c)(Code) | | Gets the minimum of three short values.
Parameters: a - value 1 Parameters: b - value 2 Parameters: c - value 3 the smallest of the values |
min | public static byte min(byte a, byte b, byte c)(Code) | | Gets the minimum of three byte values.
Parameters: a - value 1 Parameters: b - value 2 Parameters: c - value 3 the smallest of the values |
min | public static double min(double a, double b, double c)(Code) | | Gets the minimum of three double values.
If any value is NaN , NaN is
returned. Infinity is handled.
Parameters: a - value 1 Parameters: b - value 2 Parameters: c - value 3 the smallest of the values |
min | public static float min(float a, float b, float c)(Code) | | Gets the minimum of three float values.
If any value is NaN , NaN is
returned. Infinity is handled.
Parameters: a - value 1 Parameters: b - value 2 Parameters: c - value 3 the smallest of the values |
stringToInt | public static int stringToInt(String str)(Code) | | 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
Parameters: str - the string to convert, may be null the int represented by the string, or zero ifconversion failsNumberUtils.toInt(String) |
stringToInt | public static int stringToInt(String str, int defaultValue)(Code) | | 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
Parameters: str - the string to convert, may be null Parameters: defaultValue - the default value the int represented by the string, or the default if conversion failsNumberUtils.toInt(String,int) |
toDouble | public static double toDouble(String str)(Code) | | 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
Parameters: str - the string to convert, may be null the double represented by the string, or 0.0d if conversion fails since: 2.1 |
toDouble | public static double toDouble(String str, double defaultValue)(Code) | | 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
Parameters: str - the string to convert, may be null Parameters: defaultValue - the default value the double represented by the string, or defaultValueif conversion fails since: 2.1 |
toFloat | public static float toFloat(String str)(Code) | | 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
Parameters: str - the string to convert, may be null the float represented by the string, or 0.0f if conversion fails since: 2.1 |
toFloat | public static float toFloat(String str, float defaultValue)(Code) | | 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
Parameters: str - the string to convert, may be null Parameters: defaultValue - the default value the float represented by the string, or defaultValueif conversion fails since: 2.1 |
toInt | public static int toInt(String str)(Code) | | 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
Parameters: str - the string to convert, may be null the int represented by the string, or zero ifconversion fails since: 2.1 |
toInt | public static int toInt(String str, int defaultValue)(Code) | | 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
Parameters: str - the string to convert, may be null Parameters: defaultValue - the default value the int represented by the string, or the default if conversion fails since: 2.1 |
toLong | public static long toLong(String str)(Code) | | 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
Parameters: str - the string to convert, may be null the long represented by the string, or 0 ifconversion fails since: 2.1 |
toLong | public static long toLong(String str, long defaultValue)(Code) | | 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
Parameters: str - the string to convert, may be null Parameters: defaultValue - the default value the long represented by the string, or the default if conversion fails since: 2.1 |
|
|