Class NumberUtils
- Since:
- 2.0
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Byte
Reusable Byte constant for minus one.static final Byte
Reusable Byte constant for one.static final Byte
Reusable Byte constant for zero.static final Double
Reusable Double constant for minus one.static final Double
Reusable Double constant for one.static final Double
Reusable Double constant for zero.static final Float
Reusable Float constant for minus one.static final Float
Reusable Float constant for one.static final Float
Reusable Float constant for zero.static final Integer
Reusable Integer constant for minus one.static final Integer
Reusable Integer constant for one.static final Integer
Reusable Integer constant for twostatic final Integer
Reusable Integer constant for zero.static final Long
Integer.MAX_VALUE
as aLong
.static final Long
Integer.MIN_VALUE
as aLong
.static final Long
Reusable Long constant for minus one.static final Long
Reusable Long constant for one.static final Long
Reusable Long constant for zero.static final Short
Reusable Short constant for minus one.static final Short
Reusable Short constant for one.static final Short
Reusable Short constant for zero. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
compare
(byte x, byte y) Compares twobyte
values numerically.static int
compare
(int x, int y) Compares twoint
values numerically.static int
compare
(long x, long y) Compares tolong
values numerically.static int
compare
(short x, short y) Compares toshort
values numerically.static BigDecimal
createBigDecimal
(String str) Creates aBigDecimal
from aString
.static BigInteger
createBigInteger
(String str) Creates aBigInteger
from aString
.static Double
createDouble
(String str) static Float
createFloat
(String str) static Integer
createInteger
(String str) static Long
createLong
(String str) static Number
createNumber
(String str) static boolean
isCreatable
(String str) Checks whether the String is a valid Java number.static boolean
Checks whether theString
contains only digit characters.static boolean
Deprecated.This feature will be removed in Lang 4, useisCreatable(String)
insteadstatic 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 threebyte
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 threedouble
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 threefloat
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 threeint
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 threelong
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 threeshort
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 threebyte
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 threedouble
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 threefloat
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 threeint
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 threelong
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 threeshort
values.static byte
static byte
Converts aString
to abyte
, returning a default value if the conversion fails.static double
static double
Converts aString
to adouble
, returning a default value if the conversion fails.static double
toDouble
(BigDecimal value) Converts aBigDecimal
to adouble
.static double
toDouble
(BigDecimal value, double defaultValue) Converts aBigDecimal
to adouble
.static float
static float
Converts aString
to afloat
, returning a default value if the conversion fails.static int
static int
Converts aString
to anint
, returning a default value if the conversion fails.static long
static long
Converts aString
to along
, returning a default value if the conversion fails.static BigDecimal
toScaledBigDecimal
(Double value) Converts aDouble
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
.static BigDecimal
toScaledBigDecimal
(Double value, int scale, RoundingMode roundingMode) static BigDecimal
toScaledBigDecimal
(Float value) Converts aFloat
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
.static BigDecimal
toScaledBigDecimal
(Float value, int scale, RoundingMode roundingMode) static BigDecimal
toScaledBigDecimal
(String value) Converts aString
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
.static BigDecimal
toScaledBigDecimal
(String value, int scale, RoundingMode roundingMode) static BigDecimal
toScaledBigDecimal
(BigDecimal value) Converts aBigDecimal
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
.static BigDecimal
toScaledBigDecimal
(BigDecimal value, int scale, RoundingMode roundingMode) Converts aBigDecimal
to aBigDecimal
whose scale is the specified value with aRoundingMode
applied.static short
static short
Converts aString
to anshort
, returning a default value if the conversion fails.
-
Field Details
-
LONG_ZERO
Reusable Long constant for zero. -
LONG_ONE
Reusable Long constant for one. -
LONG_MINUS_ONE
Reusable Long constant for minus one. -
INTEGER_ZERO
Reusable Integer constant for zero. -
INTEGER_ONE
Reusable Integer constant for one. -
INTEGER_TWO
Reusable Integer constant for two -
INTEGER_MINUS_ONE
Reusable Integer constant for minus one. -
SHORT_ZERO
Reusable Short constant for zero. -
SHORT_ONE
Reusable Short constant for one. -
SHORT_MINUS_ONE
Reusable Short constant for minus one. -
BYTE_ZERO
Reusable Byte constant for zero. -
BYTE_ONE
Reusable Byte constant for one. -
BYTE_MINUS_ONE
Reusable Byte constant for minus one. -
DOUBLE_ZERO
Reusable Double constant for zero. -
DOUBLE_ONE
Reusable Double constant for one. -
DOUBLE_MINUS_ONE
Reusable Double constant for minus one. -
FLOAT_ZERO
Reusable Float constant for zero. -
FLOAT_ONE
Reusable Float constant for one. -
FLOAT_MINUS_ONE
Reusable Float constant for minus one. -
LONG_INT_MAX_VALUE
Integer.MAX_VALUE
as aLong
.- Since:
- 3.12.0
-
LONG_INT_MIN_VALUE
Integer.MIN_VALUE
as aLong
.- Since:
- 3.12.0
-
-
Constructor Details
-
NumberUtils
Deprecated.TODO Make private in 4.0.NumberUtils
instances should NOT be constructed in standard programming. Instead, the class should be used asNumberUtils.toInt("6");
.This constructor is public to permit tools that require a JavaBean instance to operate.
-
-
Method Details
-
compare
Compares twobyte
values numerically. This is the same functionality as provided in Java 7.- Parameters:
x
- the firstbyte
to comparey
- the secondbyte
to compare- Returns:
- the value
0
ifx == y
; a value less than0
ifx < y
; and a value greater than0
ifx > y
- Since:
- 3.4
-
compare
Compares twoint
values numerically. This is the same functionality as provided in Java 7.- Parameters:
x
- the firstint
to comparey
- the secondint
to compare- Returns:
- the value
0
ifx == y
; a value less than0
ifx < y
; and a value greater than0
ifx > y
- Since:
- 3.4
-
compare
Compares tolong
values numerically. This is the same functionality as provided in Java 7.- Parameters:
x
- the firstlong
to comparey
- the secondlong
to compare- Returns:
- the value
0
ifx == y
; a value less than0
ifx < y
; and a value greater than0
ifx > y
- Since:
- 3.4
-
compare
Compares toshort
values numerically. This is the same functionality as provided in Java 7.- Parameters:
x
- the firstshort
to comparey
- the secondshort
to compare- Returns:
- the value
0
ifx == y
; a value less than0
ifx < y
; and a value greater than0
ifx > y
- Since:
- 3.4
-
createBigDecimal
- Parameters:
str
- aString
to convert, may be null- Returns:
- converted
BigDecimal
(or null if the input is null) - Throws:
NumberFormatException
- if the value cannot be converted
-
createBigInteger
Creates aBigInteger
from aString
. Handles hexadecimal (0x or #) and octal (0) notations.Returns
null
if the string isnull
.- Parameters:
str
- aString
to convert, may be null- Returns:
- converted
BigInteger
(or null if the input is null) - Throws:
NumberFormatException
- if the value cannot be converted- Since:
- 3.2
-
createDouble
- Parameters:
str
- aString
to convert, may be null- Returns:
- converted
Double
(or null if the input is null) - Throws:
NumberFormatException
- if the value cannot be converted
-
createFloat
- Parameters:
str
- aString
to convert, may be null- Returns:
- converted
Float
(or null if the input is null) - Throws:
NumberFormatException
- if the value cannot be converted
-
createInteger
Creates anInteger
from aString
. Handles hexadecimal (0xhhhh) and octal (0dddd) notations. N.B. a leading zero means octal; spaces are not trimmed.Returns
null
if the string isnull
.- Parameters:
str
- aString
to convert, may be null- Returns:
- converted
Integer
(or null if the input is null) - Throws:
NumberFormatException
- if the value cannot be converted
-
createLong
Creates aLong
from aString
. Handles hexadecimal (0Xhhhh) and octal (0ddd) notations. N.B. a leading zero means octal; spaces are not trimmed.Returns
null
if the string isnull
.- Parameters:
str
- aString
to convert, may be null- Returns:
- converted
Long
(or null if the input is null) - Throws:
NumberFormatException
- if the value cannot be converted- Since:
- 3.1
-
createNumber
Creates aNumber
from aString
.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
toBigInteger
and fromFloat
toBigDecimal
.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 isnull
.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- Returns:
- Number created from the string (or null if the input is null)
- Throws:
NumberFormatException
- if the value cannot be converted
-
isCreatable
Checks whether the String is a valid Java number.Valid numbers include hexadecimal marked with the
0x
or0X
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 returnfalse
, since9
is not a valid octal value. However, numbers beginning with0.
are treated as decimal.null
and empty/blankString
will returnfalse
.Note,
createNumber(String)
should return a number for every input resulting intrue
.- Parameters:
str
- theString
to check- Returns:
true
if the string is a correctly formatted number- Since:
- 3.5
-
isDigits
Checks whether theString
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.This feature will be removed in Lang 4, useisCreatable(String)
insteadChecks whether the String is a valid Java number.Valid numbers include hexadecimal marked with the
0x
or0X
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 returnfalse
, since9
is not a valid octal value. However, numbers beginning with0.
are treated as decimal.null
and empty/blankString
will returnfalse
.Note,
createNumber(String)
should return a number for every input resulting intrue
.- Parameters:
str
- theString
to check- Returns:
true
if the string is a correctly formatted number- Since:
- 3.3 the code supports hexadecimal
0Xhhh
an octal0ddd
validation
-
isParsable
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)
orDouble.parseDouble(String)
. This method can be used instead of catchingParseException
when calling one of those methods.Hexadecimal and scientific notations are not considered parsable. See
isCreatable(String)
on those cases.null
and empty String will returnfalse
.- Parameters:
str
- the String to check.- Returns:
true
if the string is a parsable number.- Since:
- 3.4
-
max
Returns the maximum value in an array.- Parameters:
array
- an array, must not be null or empty- Returns:
- the maximum value in the array
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty- Since:
- 3.4 Changed signature from max(byte[]) to max(byte...)
-
max
Gets the maximum of threebyte
values.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the largest of the values
-
max
Returns the maximum value in an array.- Parameters:
array
- an array, must not be null or empty- Returns:
- the maximum value in the array
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty- Since:
- 3.4 Changed signature from max(double[]) to max(double...)
- See Also:
-
max
Gets the maximum of threedouble
values.If any value is
NaN
,NaN
is returned. Infinity is handled.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the largest of the values
- See Also:
-
max
Returns the maximum value in an array.- Parameters:
array
- an array, must not be null or empty- Returns:
- the maximum value in the array
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty- Since:
- 3.4 Changed signature from max(float[]) to max(float...)
- See Also:
-
max
Gets the maximum of threefloat
values.If any value is
NaN
,NaN
is returned. Infinity is handled.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the largest of the values
- See Also:
-
max
Returns the maximum value in an array.- Parameters:
array
- an array, must not be null or empty- Returns:
- the maximum value in the array
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty- Since:
- 3.4 Changed signature from max(int[]) to max(int...)
-
max
Gets the maximum of threeint
values.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the largest of the values
-
max
Returns the maximum value in an array.- Parameters:
array
- an array, must not be null or empty- Returns:
- the maximum value in the array
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty- Since:
- 3.4 Changed signature from max(long[]) to max(long...)
-
max
Gets the maximum of threelong
values.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the largest of the values
-
max
Returns the maximum value in an array.- Parameters:
array
- an array, must not be null or empty- Returns:
- the maximum value in the array
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty- Since:
- 3.4 Changed signature from max(short[]) to max(short...)
-
max
Gets the maximum of threeshort
values.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the largest of the values
-
min
Returns the minimum value in an array.- Parameters:
array
- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty- Since:
- 3.4 Changed signature from min(byte[]) to min(byte...)
-
min
Gets the minimum of threebyte
values.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the smallest of the values
-
min
Returns the minimum value in an array.- Parameters:
array
- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty- Since:
- 3.4 Changed signature from min(double[]) to min(double...)
- See Also:
-
min
Gets the minimum of threedouble
values.If any value is
NaN
,NaN
is returned. Infinity is handled.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the smallest of the values
- See Also:
-
min
Returns the minimum value in an array.- Parameters:
array
- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty- Since:
- 3.4 Changed signature from min(float[]) to min(float...)
- See Also:
-
min
Gets the minimum of threefloat
values.If any value is
NaN
,NaN
is returned. Infinity is handled.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the smallest of the values
- See Also:
-
min
Returns the minimum value in an array.- Parameters:
array
- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty- Since:
- 3.4 Changed signature from min(int[]) to min(int...)
-
min
Gets the minimum of threeint
values.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the smallest of the values
-
min
Returns the minimum value in an array.- Parameters:
array
- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty- Since:
- 3.4 Changed signature from min(long[]) to min(long...)
-
min
Gets the minimum of threelong
values.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the smallest of the values
-
min
Returns the minimum value in an array.- Parameters:
array
- an array, must not be null or empty- Returns:
- the minimum value in the array
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty- Since:
- 3.4 Changed signature from min(short[]) to min(short...)
-
min
Gets the minimum of threeshort
values.- Parameters:
a
- value 1b
- value 2c
- value 3- Returns:
- the smallest of the values
-
toByte
Converts aString
to abyte
, returningzero
if the conversion fails.If the string is
null
,zero
is returned.NumberUtils.toByte(null) = 0 NumberUtils.toByte("") = 0 NumberUtils.toByte("1") = 1
- Parameters:
str
- the string to convert, may be null- Returns:
- the byte represented by the string, or
zero
if conversion fails - Since:
- 2.5
-
toByte
Converts aString
to abyte
, 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
- Parameters:
str
- the string to convert, may be nulldefaultValue
- the default value- Returns:
- the byte represented by the string, or the default if conversion fails
- Since:
- 2.5
-
toDouble
Converts aBigDecimal
to adouble
.If the
BigDecimal
value
isnull
, then the specified default value is returned.NumberUtils.toDouble(null) = 0.0d NumberUtils.toDouble(BigDecimal.valueOf(8.5d)) = 8.5d
- Parameters:
value
- theBigDecimal
to convert, may benull
.- Returns:
- the double represented by the
BigDecimal
or0.0d
if theBigDecimal
isnull
. - Since:
- 3.8
-
toDouble
Converts aBigDecimal
to adouble
.If the
BigDecimal
value
isnull
, then the specified default value is returned.NumberUtils.toDouble(null, 1.1d) = 1.1d NumberUtils.toDouble(BigDecimal.valueOf(8.5d), 1.1d) = 8.5d
- Parameters:
value
- theBigDecimal
to convert, may benull
.defaultValue
- the default value- Returns:
- the double represented by the
BigDecimal
or the defaultValue if theBigDecimal
isnull
. - Since:
- 3.8
-
toDouble
Converts aString
to adouble
, returning0.0d
if the conversion fails.If the string
str
isnull
,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 benull
- Returns:
- the double represented by the string, or
0.0d
if conversion fails - Since:
- 2.1
-
toDouble
Converts aString
to adouble
, returning a default value if the conversion fails.If the string
str
isnull
, 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 benull
defaultValue
- the default value- Returns:
- the double represented by the string, or defaultValue if conversion fails
- Since:
- 2.1
-
toFloat
Converts aString
to afloat
, returning0.0f
if the conversion fails.If the string
str
isnull
,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 benull
- Returns:
- the float represented by the string, or
0.0f
if conversion fails - Since:
- 2.1
-
toFloat
Converts aString
to afloat
, returning a default value if the conversion fails.If the string
str
isnull
, the default value is returned.NumberUtils.toFloat(null, 1.1f) = 1.1f NumberUtils.toFloat("", 1.1f) = 1.1f NumberUtils.toFloat("1.5", 0.0f) = 1.5f
- Parameters:
str
- the string to convert, may benull
defaultValue
- the default value- Returns:
- the float represented by the string, or defaultValue if conversion fails
- Since:
- 2.1
-
toInt
Converts aString
to anint
, returningzero
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- Returns:
- the int represented by the string, or
zero
if conversion fails - Since:
- 2.1
-
toInt
Converts aString
to anint
, 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 nulldefaultValue
- the default value- Returns:
- the int represented by the string, or the default if conversion fails
- Since:
- 2.1
-
toLong
Converts aString
to along
, returningzero
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- Returns:
- the long represented by the string, or
0
if conversion fails - Since:
- 2.1
-
toLong
Converts aString
to along
, 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 nulldefaultValue
- the default value- Returns:
- the long represented by the string, or the default if conversion fails
- Since:
- 2.1
-
toScaledBigDecimal
Converts aBigDecimal
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
. If the suppliedvalue
is null, thenBigDecimal.ZERO
is returned.Note, the scale of a
BigDecimal
is the number of digits to the right of the decimal point.- Parameters:
value
- theBigDecimal
to convert, may be null.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Converts aBigDecimal
to aBigDecimal
whose scale is the specified value with aRoundingMode
applied. If the inputvalue
isnull
, we simply returnBigDecimal.ZERO
.- Parameters:
value
- theBigDecimal
to convert, may be null.scale
- the number of digits to the right of the decimal point.roundingMode
- a rounding behavior for numerical operations capable of discarding precision.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Converts aDouble
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
. If the suppliedvalue
is null, thenBigDecimal.ZERO
is returned.Note, the scale of a
BigDecimal
is the number of digits to the right of the decimal point.- Parameters:
value
- theDouble
to convert, may be null.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Converts aDouble
to aBigDecimal
whose scale is the specified value with aRoundingMode
applied. If the inputvalue
isnull
, we simply returnBigDecimal.ZERO
.- Parameters:
value
- theDouble
to convert, may be null.scale
- the number of digits to the right of the decimal point.roundingMode
- a rounding behavior for numerical operations capable of discarding precision.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Converts aFloat
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
. If the suppliedvalue
is null, thenBigDecimal.ZERO
is returned.Note, the scale of a
BigDecimal
is the number of digits to the right of the decimal point.- Parameters:
value
- theFloat
to convert, may be null.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Converts aFloat
to aBigDecimal
whose scale is the specified value with aRoundingMode
applied. If the inputvalue
isnull
, we simply returnBigDecimal.ZERO
.- Parameters:
value
- theFloat
to convert, may be null.scale
- the number of digits to the right of the decimal point.roundingMode
- a rounding behavior for numerical operations capable of discarding precision.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Converts aString
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
. If the suppliedvalue
is null, thenBigDecimal.ZERO
is returned.Note, the scale of a
BigDecimal
is the number of digits to the right of the decimal point.- Parameters:
value
- theString
to convert, may be null.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Converts aString
to aBigDecimal
whose scale is the specified value with aRoundingMode
applied. If the inputvalue
isnull
, we simply returnBigDecimal.ZERO
.- Parameters:
value
- theString
to convert, may be null.scale
- the number of digits to the right of the decimal point.roundingMode
- a rounding behavior for numerical operations capable of discarding precision.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toShort
Converts aString
to ashort
, returningzero
if the conversion fails.If the string is
null
,zero
is returned.NumberUtils.toShort(null) = 0 NumberUtils.toShort("") = 0 NumberUtils.toShort("1") = 1
- Parameters:
str
- the string to convert, may be null- Returns:
- the short represented by the string, or
zero
if conversion fails - Since:
- 2.5
-
toShort
Converts aString
to anshort
, 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
- Parameters:
str
- the string to convert, may be nulldefaultValue
- the default value- Returns:
- the short represented by the string, or the default if conversion fails
- Since:
- 2.5
-