public class NumberUtils extends Object
Provides extra functionality for Java Number classes.
Modifier and Type | Field and Description |
---|---|
static Byte |
BYTE_MINUS_ONE
Reusable Byte constant for minus one.
|
static Byte |
BYTE_ONE
Reusable Byte constant for one.
|
static Byte |
BYTE_ZERO
Reusable Byte constant for zero.
|
static Double |
DOUBLE_MINUS_ONE
Reusable Double constant for minus one.
|
static Double |
DOUBLE_ONE
Reusable Double constant for one.
|
static Double |
DOUBLE_ZERO
Reusable Double constant for zero.
|
static Float |
FLOAT_MINUS_ONE
Reusable Float constant for minus one.
|
static Float |
FLOAT_ONE
Reusable Float constant for one.
|
static Float |
FLOAT_ZERO
Reusable Float constant for zero.
|
static Integer |
INTEGER_MINUS_ONE
Reusable Integer constant for minus one.
|
static Integer |
INTEGER_ONE
Reusable Integer constant for one.
|
static Integer |
INTEGER_ZERO
Reusable Integer constant for zero.
|
static Long |
LONG_MINUS_ONE
Reusable Long constant for minus one.
|
static Long |
LONG_ONE
Reusable Long constant for one.
|
static Long |
LONG_ZERO
Reusable Long constant for zero.
|
static Short |
SHORT_MINUS_ONE
Reusable Short constant for minus one.
|
static Short |
SHORT_ONE
Reusable Short constant for one.
|
static Short |
SHORT_ZERO
Reusable Short constant for zero.
|
Constructor and Description |
---|
NumberUtils()
NumberUtils instances should NOT be constructed in standard programming. |
Modifier and Type | Method and Description |
---|---|
static int |
compare(byte x,
byte y)
Compares two
byte values numerically. |
static int |
compare(int x,
int y)
Compares two
int values numerically. |
static int |
compare(long x,
long y)
Compares to
long values numerically. |
static int |
compare(short x,
short y)
Compares to
short values numerically. |
static BigDecimal |
createBigDecimal(String str)
Convert a
String to a BigDecimal . |
static BigInteger |
createBigInteger(String str)
Convert a
String to a BigInteger ;
since 3.2 it handles hex (0x or #) and octal (0) notations. |
static Double |
createDouble(String str)
Convert a
String to a Double . |
static Float |
createFloat(String str)
Convert a
String to a Float . |
static Integer |
createInteger(String str)
Convert a
String to a Integer , handling
hex (0xhhhh) and octal (0dddd) notations. |
static Long |
createLong(String str)
Convert a
String to a Long ;
since 3.1 it handles hex (0Xhhhh) and octal (0ddd) notations. |
static Number |
createNumber(String str)
Turns a string value into a java.lang.Number.
|
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 boolean |
isParsable(String str)
Checks whether the given String is a parsable 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 byte |
toByte(String str)
Convert a
String to a byte , returning
zero if the conversion fails. |
static byte |
toByte(String str,
byte defaultValue)
Convert a
String to a byte , returning a
default value if the conversion fails. |
static double |
toDouble(String str)
Convert a
String to a double , returning
0.0d if the conversion fails. |
static double |
toDouble(String str,
double defaultValue)
Convert a
String to a double , returning a
default value if the conversion fails. |
static float |
toFloat(String str)
Convert a
String to a float , returning
0.0f if the conversion fails. |
static float |
toFloat(String str,
float defaultValue)
Convert a
String to a float , returning a
default value if the conversion fails. |
static int |
toInt(String str)
Convert a
String to an int , returning
zero if the conversion fails. |
static int |
toInt(String str,
int defaultValue)
Convert a
String to an int , returning a
default value if the conversion fails. |
static long |
toLong(String str)
Convert a
String to a long , returning
zero if the conversion fails. |
static long |
toLong(String str,
long defaultValue)
Convert a
String to a long , returning a
default value if the conversion fails. |
static short |
toShort(String str)
Convert a
String to a short , returning
zero if the conversion fails. |
static short |
toShort(String str,
short defaultValue)
Convert a
String to an short , returning a
default value if the conversion fails. |
public static final Long LONG_ZERO
public static final Long LONG_ONE
public static final Long LONG_MINUS_ONE
public static final Integer INTEGER_ZERO
public static final Integer INTEGER_ONE
public static final Integer INTEGER_MINUS_ONE
public static final Short SHORT_ZERO
public static final Short SHORT_ONE
public static final Short SHORT_MINUS_ONE
public static final Byte BYTE_ZERO
public static final Byte BYTE_ONE
public static final Byte BYTE_MINUS_ONE
public static final Double DOUBLE_ZERO
public static final Double DOUBLE_ONE
public static final Double DOUBLE_MINUS_ONE
public static final Float FLOAT_ZERO
public static final Float FLOAT_ONE
public static final Float FLOAT_MINUS_ONE
public NumberUtils()
NumberUtils
instances should NOT be constructed in standard programming.
Instead, the class should be used as NumberUtils.toInt("6");
.
This constructor is public to permit tools that require a JavaBean instance to operate.
public static int toInt(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 nullzero
if
conversion failspublic static int toInt(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 valuepublic static long toLong(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 null0
if
conversion failspublic static long toLong(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 valuepublic static float toFloat(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(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 valuepublic static double toDouble(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(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 valuepublic static byte toByte(String str)
Convert a String
to a byte
, returning
zero
if the conversion fails.
If the string is null
, zero
is returned.
NumberUtils.toByte(null) = 0 NumberUtils.toByte("") = 0 NumberUtils.toByte("1") = 1
str
- the string to convert, may be nullzero
if
conversion failspublic static byte toByte(String str, byte defaultValue)
Convert a String
to a byte
, 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
str
- the string to convert, may be nulldefaultValue
- the default valuepublic static short toShort(String str)
Convert a String
to a short
, returning
zero
if the conversion fails.
If the string is null
, zero
is returned.
NumberUtils.toShort(null) = 0 NumberUtils.toShort("") = 0 NumberUtils.toShort("1") = 1
str
- the string to convert, may be nullzero
if
conversion failspublic static short toShort(String str, short defaultValue)
Convert a String
to an short
, 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
str
- the string to convert, may be nulldefaultValue
- the default valuepublic static Number createNumber(String str) throws NumberFormatException
Turns a string value into a java.lang.Number.
If the string starts with 0x
or -0x
(lower or upper case) or #
or -#
, it
will be interpreted as a hexadecimal Integer - or Long, if the number of digits after the
prefix is more than 8 - or BigInteger if there are more than 16 digits.
Then, the value is examined for a type qualifier on the end, i.e. one of
'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
.
Integral values with a leading 0
will be interpreted as octal; the returned number will
be Integer, Long or BigDecimal as appropriate.
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 nullNumberFormatException
- if the value cannot be convertedpublic static Float createFloat(String str)
Convert a String
to a Float
.
Returns null
if the string is null
.
str
- a String
to convert, may be nullFloat
(or null if the input is null)NumberFormatException
- if the value cannot be convertedpublic static Double createDouble(String str)
Convert a String
to a Double
.
Returns null
if the string is null
.
str
- a String
to convert, may be nullDouble
(or null if the input is null)NumberFormatException
- if the value cannot be convertedpublic static Integer createInteger(String str)
Convert a String
to a Integer
, handling
hex (0xhhhh) and octal (0dddd) notations.
N.B. a leading zero means octal; spaces are not trimmed.
Returns null
if the string is null
.
str
- a String
to convert, may be nullInteger
(or null if the input is null)NumberFormatException
- if the value cannot be convertedpublic static Long createLong(String str)
Convert a String
to a Long
;
since 3.1 it handles hex (0Xhhhh) and octal (0ddd) notations.
N.B. a leading zero means octal; spaces are not trimmed.
Returns null
if the string is null
.
str
- a String
to convert, may be nullLong
(or null if the input is null)NumberFormatException
- if the value cannot be convertedpublic static BigInteger createBigInteger(String str)
Convert a String
to a BigInteger
;
since 3.2 it handles hex (0x or #) and octal (0) notations.
Returns null
if the string is null
.
str
- a String
to convert, may be nullBigInteger
(or null if the input is null)NumberFormatException
- if the value cannot be convertedpublic static BigDecimal createBigDecimal(String str)
Convert a String
to a BigDecimal
.
Returns null
if the string is null
.
str
- a String
to convert, may be nullBigDecimal
(or null if the input is null)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 emptyIllegalArgumentException
- if array
is null
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 emptyIllegalArgumentException
- if array
is null
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 emptyIllegalArgumentException
- if array
is null
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 emptyIllegalArgumentException
- if array
is null
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 emptyIllegalArgumentException
- if array
is null
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 emptyIllegalArgumentException
- if array
is null
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 emptyIllegalArgumentException
- if array
is null
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 emptyIllegalArgumentException
- if array
is null
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 emptyIllegalArgumentException
- if array
is null
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 emptyIllegalArgumentException
- if array
is null
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 emptyIllegalArgumentException
- if array
is null
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 emptyIllegalArgumentException
- if array
is null
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 3public static int min(int a, int b, int c)
Gets the minimum of three int
values.
a
- value 1b
- value 2c
- value 3public static short min(short a, short b, short c)
Gets the minimum of three short
values.
a
- value 1b
- value 2c
- value 3public static byte min(byte a, byte b, byte c)
Gets the minimum of three byte
values.
a
- value 1b
- value 2c
- value 3public 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 3for 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 3for 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 3public static int max(int a, int b, int c)
Gets the maximum of three int
values.
a
- value 1b
- value 2c
- value 3public static short max(short a, short b, short c)
Gets the maximum of three short
values.
a
- value 1b
- value 2c
- value 3public static byte max(byte a, byte b, byte c)
Gets the maximum of three byte
values.
a
- value 1b
- value 2c
- value 3public 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 3for 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 3for a version of this method that handles NaN differently
public static boolean isDigits(String str)
Checks whether the String
contains only
digit characters.
Null
and empty String will return
false
.
str
- the String
to checktrue
if str contains only Unicode numericpublic static boolean isNumber(String str)
Checks whether the String a valid Java number.
Valid numbers include hexadecimal marked with the 0x
or
0X
qualifier, octal numbers, scientific notation and numbers
marked with a type qualifier (e.g. 123L).
Non-hexadecimal strings beginning with a leading zero are
treated as octal values. Thus the string 09
will return
false
, since 9
is not a valid octal value.
However, numbers beginning with 0.
are treated as decimal.
null
and empty/blank String
will return
false
.
str
- the String
to checktrue
if the string is a correctly formatted number0Xhhh
and octal 0ddd
validationpublic static boolean isParsable(String str)
Checks whether the given String is a parsable number.
Parsable numbers include those Strings understood by Integer.parseInt(String)
,
Long.parseLong(String)
, Float.parseFloat(String)
or
Double.parseDouble(String)
. This method can be used instead of catching ParseException
when calling one of those methods.
Hexadecimal and scientific notations are not considered parsable.
See isNumber(String)
on those cases.
Null
and empty String will return false
.
str
- the String to check.true
if the string is a parsable number.public static int compare(int x, int y)
Compares two int
values numerically. This is the same functionality as provided in Java 7.
x
- the first int
to comparey
- the second int
to compare0
if x == y
;
a value less than 0
if x < y
; and
a value greater than 0
if x > y
public static int compare(long x, long y)
Compares to long
values numerically. This is the same functionality as provided in Java 7.
x
- the first long
to comparey
- the second long
to compare0
if x == y
;
a value less than 0
if x < y
; and
a value greater than 0
if x > y
public static int compare(short x, short y)
Compares to short
values numerically. This is the same functionality as provided in Java 7.
x
- the first short
to comparey
- the second short
to compare0
if x == y
;
a value less than 0
if x < y
; and
a value greater than 0
if x > y
public static int compare(byte x, byte y)
Compares two byte
values numerically. This is the same functionality as provided in Java 7.
x
- the first byte
to comparey
- the second byte
to compare0
if x == y
;
a value less than 0
if x < y
; and
a value greater than 0
if x > y
Copyright © 2001–2015 The Apache Software Foundation. All rights reserved.