Top Description Fields Constructors Methods
org.apache.commons.lang3.math

public Class NumberUtils

extends Object
Class Inheritance
Imports
java.lang.reflect.Array, java.math.BigDecimal, .BigInteger, .RoundingMode, java.util.Objects, org.apache.commons.lang3.CharUtils, .StringUtils, .Validate

Provides extra functionality for Java Number classes.
Since
2.0

Field Summary

Modifier and TypeField and Description
public static final Byte
BYTE_MINUS_ONE

Reusable Byte constant for minus one.

public static final Byte
BYTE_ONE

Reusable Byte constant for one.

public static final Byte
BYTE_ZERO

Reusable Byte constant for zero.

public static final Double
DOUBLE_MINUS_ONE

Reusable Double constant for minus one.

public static final Double
DOUBLE_ONE

Reusable Double constant for one.

public static final Double
DOUBLE_ZERO

Reusable Double constant for zero.

public static final Float
FLOAT_MINUS_ONE

Reusable Float constant for minus one.

public static final Float
FLOAT_ONE

Reusable Float constant for one.

public static final Float
FLOAT_ZERO

Reusable Float constant for zero.

public static final Integer
INTEGER_MINUS_ONE

Reusable Integer constant for minus one.

public static final Integer
INTEGER_ONE

Reusable Integer constant for one.

public static final Integer
INTEGER_TWO

Reusable Integer constant for two

public static final Integer
INTEGER_ZERO

Reusable Integer constant for zero.

public static final Long
public static final Long
public static final Long
LONG_MINUS_ONE

Reusable Long constant for minus one.

public static final Long
LONG_ONE

Reusable Long constant for one.

public static final Long
LONG_ZERO

Reusable Long constant for zero.

public static final Short
SHORT_MINUS_ONE

Reusable Short constant for minus one.

public static final Short
SHORT_ONE

Reusable Short constant for one.

public static final Short
SHORT_ZERO

Reusable Short constant for zero.

Constructor Summary

AccessConstructor and Description
public
NumberUtils()
Deprecated TODO Make private in 4.0.

NumberUtils instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
public static int

Returns:

the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y.
compare
(final byte
the first byte to compare.
x
,
final byte
the second byte to compare.
y
)
Deprecated Use Byte#compare(byte, byte).

Compares two byte values numerically.

public static int

Returns:

the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y.
compare
(final int
the first int to compare.
x
,
final int
the second int to compare.
y
)
Deprecated Use Integer#compare(int, int).

Compares two int values numerically.

public static int

Returns:

the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y.
compare
(final long
the first long to compare.
x
,
final long
the second long to compare.
y
)
Deprecated Use Long#compare(long, long).

Compares to long values numerically.

public static int

Returns:

the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y.
compare
(final short
the first short to compare.
x
,
final short
the second short to compare.
y
)
Deprecated Use Short#compare(short, short).

Compares to short values numerically.

public static BigDecimal

Returns:

converted BigDecimal (or null if the input is null).
createBigDecimal
(final String
a String to convert, may be null.
str
)

Creates a BigDecimal from a String.

public static BigInteger

Returns:

converted BigInteger (or null if the input is null).
createBigInteger
(final String
a String to convert, may be null.
str
)

Creates a BigInteger from a String.

public static Double

Returns:

converted Double (or null if the input is null).
createDouble
(final String
a String to convert, may be null.
str
)

Creates a Double from a String.

public static Float

Returns:

converted Float (or null if the input is null).
createFloat
(final String
a String to convert, may be null.
str
)

Creates a Float from a String.

public static Integer

Returns:

converted Integer (or null if the input is null).
createInteger
(final String
a String to convert, may be null.
str
)

Creates an Integer from a String.

public static Long

Returns:

converted Long (or null if the input is null).
createLong
(final String
a String to convert, may be null.
str
)

Creates a Long from a String.

public static Number

Returns:

Number created from the string (or null if the input is null).
createNumber
(final String
String containing a number, may be null.
str
)

Creates a Number from a String.

private static String

Returns:

mantissa of the given number.
getMantissa
(final String
the string representation of the number.
str
,
final int
the position of the exponent or decimal point.
stopPos
)

Utility method for createNumber(String).

private static boolean

Returns:

if it is all zeros or null.
isAllZeros
(final String
the String to check.
str
)

Utility method for createNumber(java.lang.String).

public static boolean

Returns:

true if the string is a correctly formatted number.
isCreatable
(final String
the String to check.
str
)

Checks whether the String is a valid Java number.

public static boolean

Returns:

true if str contains only Unicode numeric
isDigits
(final String
the String to check
str
)

Checks whether the String contains only digit characters.

public static boolean

Returns:

true if the string is a correctly formatted number.
isNumber
(final String
the String to check.
str
)
Deprecated This feature will be removed in Lang 4, use NumberUtils#isCreatable(String) instead.

Checks whether the String is a valid Java number.

public static boolean

Returns:

true if the string is a parsable number.
isParsable
(final String
the String to check.
str
)

Checks whether the given String is a parsable number.

private static boolean
isSign(final char ch)

private static boolean

Returns:

true if the magnitude is zero.
isZero
(final String
the mantissa decimal digits before the decimal point (sign must be removed; never null).
mant
,
final String
the decimal digits after the decimal point (exponent and type specifier removed; can be null)
dec
)

Utility method for createNumber(java.lang.String).

public static byte

Returns:

the maximum value in the array.
max
(final byte...
an array, must not be null or empty.
array
)

Returns the maximum value in an array.

public static byte

Returns:

the largest of the values.
max
(byte
value 1.
a
,
final byte
value 2.
b
,
final byte
value 3.
c
)

Gets the maximum of three byte values.

public static double

Returns:

the maximum value in the array.
max
(final double...
an array, must not be null or empty.
array
)

Returns the maximum value in an array.

public static double

Returns:

the largest of the values.
max
(final double
value 1.
a
,
final double
value 2.
b
,
final double
value 3.
c
)

Gets the maximum of three double values.

public static float

Returns:

the maximum value in the array.
max
(final float...
an array, must not be null or empty.
array
)

Returns the maximum value in an array.

public static float

Returns:

the largest of the values.
max
(final float
value 1.
a
,
final float
value 2.
b
,
final float
value 3.
c
)

Gets the maximum of three float values.

public static int

Returns:

the maximum value in the array.
max
(final int...
an array, must not be null or empty.
array
)

Returns the maximum value in an array.

public static int

Returns:

the largest of the values.
max
(int
value 1.
a
,
final int
value 2.
b
,
final int
value 3.
c
)

Gets the maximum of three int values.

public static long

Returns:

the maximum value in the array.
max
(final long...
an array, must not be null or empty.
array
)

Returns the maximum value in an array.

public static long

Returns:

the largest of the values.
max
(long
value 1.
a
,
final long
value 2.
b
,
final long
value 3.
c
)

Gets the maximum of three long values.

public static short

Returns:

the maximum value in the array.
max
(final short...
an array, must not be null or empty.
array
)

Returns the maximum value in an array.

public static short

Returns:

the largest of the values.
max
(short
value 1.
a
,
final short
value 2.
b
,
final short
value 3.
c
)

Gets the maximum of three short values.

public static byte

Returns:

the minimum value in the array.
min
(final byte...
an array, must not be null or empty.
array
)

Returns the minimum value in an array.

public static byte

Returns:

the smallest of the values.
min
(byte
value 1.
a
,
final byte
value 2.
b
,
final byte
value 3.
c
)

Gets the minimum of three byte values.

public static double

Returns:

the minimum value in the array.
min
(final double...
an array, must not be null or empty.
array
)

Returns the minimum value in an array.

public static double

Returns:

the smallest of the values.
min
(final double
value 1.
a
,
final double
value 2.
b
,
final double
value 3.
c
)

Gets the minimum of three double values.

public static float

Returns:

the minimum value in the array.
min
(final float...
an array, must not be null or empty.
array
)

Returns the minimum value in an array.

public static float

Returns:

the smallest of the values.
min
(final float
value 1.
a
,
final float
value 2.
b
,
final float
value 3.
c
)

Gets the minimum of three float values.

public static int

Returns:

the minimum value in the array.
min
(final int...
an array, must not be null or empty.
array
)

Returns the minimum value in an array.

public static int

Returns:

the smallest of the values.
min
(int
value 1.
a
,
final int
value 2.
b
,
final int
value 3.
c
)

Gets the minimum of three int values.

public static long

Returns:

the minimum value in the array.
min
(final long...
an array, must not be null or empty.
array
)

Returns the minimum value in an array.

public static long

Returns:

the smallest of the values.
min
(long
value 1.
a
,
final long
value 2.
b
,
final long
value 3.
c
)

Gets the minimum of three long values.

public static short

Returns:

the minimum value in the array.
min
(final short...
an array, must not be null or empty.
array
)

Returns the minimum value in an array.

public static short

Returns:

the smallest of the values.
min
(short
value 1.
a
,
final short
value 2.
b
,
final short
value 3.
c
)

Gets the minimum of three short values.

public static byte

Returns:

the byte represented by the string, or zero if conversion fails.
toByte
(final String
the string to convert, may be null.
str
)

Converts a String to a byte, returning zero if the conversion fails.

public static byte

Returns:

the byte represented by the string, or the default if conversion fails.
toByte
(final String
the string to convert, may be null.
str
,
final byte
the default value.
defaultValue
)

Converts a String to a byte, returning a default value if the conversion fails.

public static double

Returns:

the double represented by the BigDecimal or 0.0d if the BigDecimal is null.
toDouble
(final BigDecimal
the BigDecimal to convert, may be null.
value
)

Converts a BigDecimal to a double.

public static double

Returns:

the double represented by the BigDecimal or the defaultValue if the BigDecimal is null.
toDouble
(final BigDecimal
the BigDecimal to convert, may be null.
value
,
final double
the default value.
defaultValue
)

Converts a BigDecimal to a double.

public static double

Returns:

the double represented by the string, or 0.0d if conversion fails.
toDouble
(final String
the string to convert, may be null.
str
)

Converts a String to a double, returning 0.0d if the conversion fails.

public static double

Returns:

the double represented by the string, or defaultValue if conversion fails.
toDouble
(final String
the string to convert, may be null
str
,
final double
the default value.
defaultValue
)

Converts a String to a double, returning a default value if the conversion fails.

public static float

Returns:

the float represented by the string, or 0.0f if conversion fails.
toFloat
(final String
the string to convert, may be null.
str
)

Converts a String to a float, returning 0.0f if the conversion fails.

public static float

Returns:

the float represented by the string, or defaultValue if conversion fails.
toFloat
(final String
the string to convert, may be null.
str
,
final float
the default value.
defaultValue
)

Converts a String to a float, returning a default value if the conversion fails.

public static int

Returns:

the int represented by the string, or zero if conversion fails.
toInt
(final String
the string to convert, may be null.
str
)

Converts a String to an int, returning zero if the conversion fails.

public static int

Returns:

the int represented by the string, or the default if conversion fails.
toInt
(final String
the string to convert, may be null.
str
,
final int
the default value.
defaultValue
)

Converts a String to an int, returning a default value if the conversion fails.

public static long

Returns:

the long represented by the string, or 0 if conversion fails.
toLong
(final String
the string to convert, may be null.
str
)

Converts a String to a long, returning zero if the conversion fails.

public static long

Returns:

the long represented by the string, or the default if conver sion fails.
toLong
(final String
the string to convert, may be null.
str
,
final long
the default value.
defaultValue
)

Converts a String to a long, returning a default value if the conversion fails.

public static BigDecimal

Returns:

the scaled, with appropriate rounding, BigDecimal.
toScaledBigDecimal
(final BigDecimal
the BigDecimal to convert, may be null.
value
)

Converts a BigDecimal to a BigDecimal with a scale of two that has been rounded using RoundingMode.HALF_EVEN.

public static BigDecimal

Returns:

the scaled, with appropriate rounding, BigDecimal.
toScaledBigDecimal
(final BigDecimal
the BigDecimal to convert, may be null.
value
,
final int
the number of digits to the right of the decimal point.
scale
,
final RoundingMode
a rounding behavior for numerical operations capable of discarding precision.
roundingMode
)

Converts a BigDecimal to a BigDecimal whose scale is the specified value with a RoundingMode applied.

public static BigDecimal

Returns:

the scaled, with appropriate rounding, BigDecimal.
toScaledBigDecimal
(final Double
the Double to convert, may be null.
value
)

Converts a Double to a BigDecimal with a scale of two that has been rounded using RoundingMode.HALF_EVEN.

public static BigDecimal

Returns:

the scaled, with appropriate rounding, BigDecimal.
toScaledBigDecimal
(final Double
the Double to convert, may be null.
value
,
final int
the number of digits to the right of the decimal point.
scale
,
final RoundingMode
a rounding behavior for numerical operations capable of discarding precision.
roundingMode
)

Converts a Double to a BigDecimal whose scale is the specified value with a RoundingMode applied.

public static BigDecimal

Returns:

the scaled, with appropriate rounding, BigDecimal.
toScaledBigDecimal
(final Float
the Float to convert, may be null.
value
)

Converts a Float to a BigDecimal with a scale of two that has been rounded using RoundingMode.HALF_EVEN.

public static BigDecimal

Returns:

the scaled, with appropriate rounding, BigDecimal.
toScaledBigDecimal
(final Float
the Float to convert, may be null.
value
,
final int
the number of digits to the right of the decimal point.
scale
,
final RoundingMode
a rounding behavior for numerical operations capable of discarding precision.
roundingMode
)

Converts a Float to a BigDecimal whose scale is the specified value with a RoundingMode applied.

public static BigDecimal

Returns:

the scaled, with appropriate rounding, BigDecimal.
toScaledBigDecimal
(final String
the String to convert, may be null.
value
)

Converts a String to a BigDecimal with a scale of two that has been rounded using RoundingMode.HALF_EVEN.

public static BigDecimal

Returns:

the scaled, with appropriate rounding, BigDecimal.
toScaledBigDecimal
(final String
the String to convert, may be null.
value
,
final int
the number of digits to the right of the decimal point.
scale
,
final RoundingMode
a rounding behavior for numerical operations capable of discarding precision.
roundingMode
)

Converts a String to a BigDecimal whose scale is the specified value with a RoundingMode applied.

public static short

Returns:

the short represented by the string, or zero if conversion fails.
toShort
(final String
the string to convert, may be null.
str
)

Converts a String to a short, returning zero if the conversion fails.

public static short

Returns:

the short represented by the string, or the default if conversion fails.
toShort
(final String
the string to convert, may be null.
str
,
final short
the default value.
defaultValue
)

Converts a String to an short, returning a default value if the conversion fails.

private static void
validateArray(final Object
the array to check.
array
)

Checks if the specified array is neither null nor empty.

private static boolean
withDecimalsParsing(final String str, final int beginIdx)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait