|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.commons.lang.NumberUtils
Provides extra functionality for Java Number classes.
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 BigDecimal |
createBigDecimal(String val)
Convert a String to a BigDecimal |
static BigInteger |
createBigInteger(String val)
Convert a String to a BigInteger |
static Double |
createDouble(String val)
Convert a String to a Double |
static Float |
createFloat(String val)
Convert a String to a Float |
static Integer |
createInteger(String val)
Convert a String to a Integer, handling hex and octal notations. |
static Long |
createLong(String val)
Convert a String to a Long |
static Number |
createNumber(String val)
Turns a string value into a java.lang.Number. |
private static boolean |
isAllZeros(String s)
Utility method for createNumber. |
static boolean |
isDigits(String str)
Checks whether the String contains only digit characters. |
static boolean |
isNumber(String str)
Checks whether the String a valid Java number. |
static int |
maximum(int a,
int b,
int c)
Gets the maximum of three int values. |
static long |
maximum(long a,
long b,
long c)
Gets the maximum of three long values. |
static int |
minimum(int a,
int b,
int c)
Gets the minimum of three int values. |
static long |
minimum(long a,
long b,
long c)
Gets the minimum of three long values. |
static int |
stringToInt(String str)
Convert a String to an int, returning zero if the conversion fails |
static int |
stringToInt(String str,
int defaultValue)
Convert a String to an int, 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 |
Constructor Detail |
public NumberUtils()
NumberUtils.stringToInt("6");
.
This constructor is public to permit tools that require a JavaBean instance
to operate.
Method Detail |
public static int stringToInt(String str)
str
- the string to convert
public static int stringToInt(String str, int defaultValue)
str
- the string to convertdefaultValue
- the default value
public static Number createNumber(String val) throws 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 succissively larger types from the type specified
until one is found that can hold 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.
val
- String containing a number
NumberFormatException
- if the value cannot be convertedprivate static boolean isAllZeros(String s)
s
- the String to check
public static Float createFloat(String val)
val
- a String to convert
NumberFormatException
- if the value cannot be convertedpublic static Double createDouble(String val)
val
- a String to convert
NumberFormatException
- if the value cannot be convertedpublic static Integer createInteger(String val)
val
- a String to convert
NumberFormatException
- if the value cannot be convertedpublic static Long createLong(String val)
val
- a String to convert
NumberFormatException
- if the value cannot be convertedpublic static BigInteger createBigInteger(String val)
val
- a String to convert
NumberFormatException
- if the value cannot be convertedpublic static BigDecimal createBigDecimal(String val)
val
- a String to convert
NumberFormatException
- if the value cannot be convertedpublic static long minimum(long a, long b, long c)
a
- value 1b
- value 2c
- value 3
public static int minimum(int a, int b, int c)
a
- value 1b
- value 2c
- value 3
public static long maximum(long a, long b, long c)
a
- value 1b
- value 2c
- value 3
public static int maximum(int a, int b, int c)
a
- value 1b
- value 2c
- value 3
public static int compare(double lhs, double rhs)
This method is more comprhensive 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:
lhs
- the first doublerhs
- the second double
public static int compare(float lhs, float rhs)
This method is more comprhensive 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:
lhs
- the first floatrhs
- the second float
public static boolean isDigits(String str)
str
- the string to check
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. 123L).
Null and blank string will return false.
str
- the string to check
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |