Class NumberUtils
Provides extra functionality for Java Number classes.
- Since:
- 1.0
- Version:
- $Id: NumberUtils.java 905636 2010-02-02 14:03:32Z niallp $
-
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.NumberUtils
instances should NOT be constructed in standard programming. -
Method Summary
Modifier and TypeMethodDescriptionstatic int
compare
(double lhs, double rhs) Deprecated.Compares twodoubles
for order.static int
compare
(float lhs, float rhs) Deprecated.Compares two floats for order.static BigDecimal
createBigDecimal
(String val) Deprecated.Convert aString
to aBigDecimal
.static BigInteger
createBigInteger
(String val) Deprecated.Convert aString
to aBigInteger
.static Double
createDouble
(String val) Deprecated.Convert aString
to aDouble
.static Float
createFloat
(String val) Deprecated.Convert aString
to aFloat
.static Integer
createInteger
(String val) Deprecated.Convert aString
to aInteger
, handling hex and octal notations.static Long
createLong
(String val) Deprecated.Convert aString
to aLong
.static Number
createNumber
(String val) Deprecated.Turns a string value into a java.lang.Number.static boolean
Deprecated.Checks whether theString
contains only digit characters.static boolean
Deprecated.Checks whether the String a valid Java number.static int
maximum
(int a, int b, int c) Deprecated.Gets the maximum of threeint
values.static long
maximum
(long a, long b, long c) Deprecated.Gets the maximum of threelong
values.static int
minimum
(int a, int b, int c) Deprecated.Gets the minimum of threeint
values.static long
minimum
(long a, long b, long c) Deprecated.Gets the minimum of threelong
values.static int
stringToInt
(String str) Deprecated.Convert aString
to anint
, returningzero
if the conversion fails.static int
stringToInt
(String str, int defaultValue) Deprecated.Convert aString
to anint
, returning a default value if the conversion fails.
-
Constructor Details
-
NumberUtils
public NumberUtils()Deprecated.NumberUtils
instances should NOT be constructed in standard programming. Instead, the class should be used asNumberUtils.stringToInt("6");
.This constructor is public to permit tools that require a JavaBean instance to operate.
-
-
Method Details
-
stringToInt
Deprecated.Convert a
String
to anint
, returningzero
if the conversion fails.- Parameters:
str
- the string to convert- Returns:
- the int represented by the string, or
zero
if conversion fails
-
stringToInt
Deprecated.Convert a
String
to anint
, returning a default value if the conversion fails.- Parameters:
str
- the string to convertdefaultValue
- the default value- Returns:
- the int represented by the string, or the default if conversion fails
-
createNumber
Deprecated.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 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
toBigInteger
and fromFloat
toBigDecimal
.If the string starts with
0x
or-0x
, it will be interpreted as a hexadecimal integer. Values with leading0
's will not be interpreted as octal.- Parameters:
val
- String containing a number- Returns:
- Number created from the string
- Throws:
NumberFormatException
- if the value cannot be converted
-
createFloat
Deprecated.Convert a
String
to aFloat
.- Parameters:
val
- aString
to convert- Returns:
- converted
Float
- Throws:
NumberFormatException
- if the value cannot be converted
-
createDouble
Deprecated.Convert a
String
to aDouble
.- Parameters:
val
- aString
to convert- Returns:
- converted
Double
- Throws:
NumberFormatException
- if the value cannot be converted
-
createInteger
Deprecated.Convert a
String
to aInteger
, handling hex and octal notations.- Parameters:
val
- aString
to convert- Returns:
- converted
Integer
- Throws:
NumberFormatException
- if the value cannot be converted
-
createLong
Deprecated.Convert a
String
to aLong
.- Parameters:
val
- aString
to convert- Returns:
- converted
Long
- Throws:
NumberFormatException
- if the value cannot be converted
-
createBigInteger
Deprecated.Convert a
String
to aBigInteger
.- Parameters:
val
- aString
to convert- Returns:
- converted
BigInteger
- Throws:
NumberFormatException
- if the value cannot be converted
-
createBigDecimal
Deprecated.Convert a
String
to aBigDecimal
.- Parameters:
val
- aString
to convert- Returns:
- converted
BigDecimal
- Throws:
NumberFormatException
- if the value cannot be converted
-
minimum
public static long minimum(long a, long b, long c) Deprecated.Gets the minimum of three
long
values.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the smallest of the values
-
minimum
public static int minimum(int a, int b, int c) Deprecated.Gets the minimum of three
int
values.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the smallest of the values
-
maximum
public static long maximum(long a, long b, long c) Deprecated.Gets the maximum of three
long
values.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the largest of the values
-
maximum
public static int maximum(int a, int b, int c) Deprecated.Gets the maximum of three
int
values.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the largest of the values
-
compare
public static int compare(double lhs, double rhs) Deprecated.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
withNaN
will return0
.- Parameters:
lhs
- the firstdouble
rhs
- the seconddouble
- Returns:
-1
if lhs is less,+1
if greater,0
if equal to rhs
- It returns
-
compare
public static int compare(float lhs, float rhs) Deprecated.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
withNaN
will return0
.- Parameters:
lhs
- the firstfloat
rhs
- the secondfloat
- Returns:
-1
if lhs is less,+1
if greater,0
if equal to rhs
- It returns
-
isDigits
Deprecated.Checks whether the
String
contains only digit characters.Null
and empty String will returnfalse
.- Parameters:
str
- theString
to check- Returns:
true
if str contains only unicode numeric
-
isNumber
Deprecated.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 returnfalse
.- Parameters:
str
- theString
to check- Returns:
true
if the string is a correctly formatted number
-