NumberUtils
- public class org.apache.commons.lang3.math.NumberUtils[source]
Provides extra functionality for Java Number classes.
- Since:
2.0
Constructors
Deprecated. |
- @Deprecated public NumberUtils()[source]
Deprecated: TODO Make private in 4.0.
NumberUtilsinstances 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.
Fields and constants
Reusable Byte constant for minus one. |
|
Reusable Byte constant for one. |
|
Reusable Byte constant for zero. |
|
Reusable Double constant for minus one. |
|
Reusable Double constant for one. |
|
Reusable Double constant for zero. |
|
Reusable Float constant for minus one. |
|
Reusable Float constant for one. |
|
Reusable Float constant for zero. |
|
Reusable Integer constant for minus one. |
|
Reusable Integer constant for one. |
|
Reusable Integer constant for two |
|
Reusable Integer constant for zero. |
|
|
|
|
|
Reusable Long constant for minus one. |
|
Reusable Long constant for one. |
|
Reusable Long constant for zero. |
|
Reusable Short constant for minus one. |
|
Reusable Short constant for one. |
|
Reusable Short constant for zero. |
- public static final Long LONG_INT_MAX_VALUE[source]
Integer#MAX_VALUEas aLong.- Since:
3.12.0
- public static final Long LONG_INT_MIN_VALUE[source]
Integer#MIN_VALUEas aLong.- Since:
3.12.0
Methods
Deprecated. Compares two |
|
Deprecated. Compares two |
|
Deprecated. Compares to |
|
Deprecated. Compares to |
|
Creates a |
|
Creates a |
|
Tests whether the String is a valid Java number. |
|
Tests whether the |
|
Deprecated. Tests whether the String is a valid Java number. |
|
Tests whether the given String is a parsable number. |
|
Gets the maximum of three |
|
Returns the maximum value in an array. |
|
Gets the maximum of three |
|
Returns the maximum value in an array. |
|
Gets the maximum of three |
|
Returns the maximum value in an array. |
|
Gets the maximum of three |
|
Returns the maximum value in an array. |
|
Gets the maximum of three |
|
Returns the maximum value in an array. |
|
Gets the maximum of three |
|
Returns the maximum value in an array. |
|
Gets the minimum of three |
|
Returns the minimum value in an array. |
|
Gets the minimum of three |
|
Returns the minimum value in an array. |
|
Gets the minimum of three |
|
Returns the minimum value in an array. |
|
Gets the minimum of three |
|
Returns the minimum value in an array. |
|
Gets the minimum of three |
|
Returns the minimum value in an array. |
|
Gets the minimum of three |
|
Returns the minimum value in an array. |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
|
Converts a |
- @Deprecated public static int compare(byte x, byte y)[source]
Deprecated: Use
Byte#compare(byte, byte).Compares two
bytevalues numerically. This is the same functionality as provided in Java 7.- Parameters:
x – The first
byteto compare.y – The second
byteto compare.
- Returns:
The value
0ifx == y; a value less than0ifx < y; and a value greater than0ifx > y.- Since:
3.4
- @Deprecated public static int compare(int x, int y)[source]
Deprecated: Use
Integer#compare(int, int).Compares two
intvalues numerically. This is the same functionality as provided in Java 7.- Parameters:
x – The first
intto compare.y – The second
intto compare.
- Returns:
The value
0ifx == y; a value less than0ifx < y; and a value greater than0ifx > y.- Since:
3.4
- @Deprecated public static int compare(long x, long y)[source]
Deprecated: Use
Long#compare(long, long).Compares to
longvalues numerically. This is the same functionality as provided in Java 7.- Parameters:
x – The first
longto compare.y – The second
longto compare.
- Returns:
The value
0ifx == y; a value less than0ifx < y; and a value greater than0ifx > y.- Since:
3.4
- @Deprecated public static int compare(short x, short y)[source]
Deprecated: Use
Short#compare(short, short).Compares to
shortvalues numerically. This is the same functionality as provided in Java 7.- Parameters:
x – The first
shortto compare.y – The second
shortto compare.
- Returns:
The value
0ifx == y; a value less than0ifx < y; and a value greater than0ifx > y.- Since:
3.4
- public static BigDecimal createBigDecimal(String str)[source]
Creates a
BigDecimalfrom aString.Returns
nullif the string isnull.- Parameters:
str – A
Stringto convert, may be null.Return- Returns:
converted
BigDecimal(or null if the input is null).- Throws:
java.lang.NumberFormatException – Thrown if the value cannot be converted.
- public static BigInteger createBigInteger(String str)[source]
Creates a
BigIntegerfrom aString. Handles hexadecimal (0x or #) and octal (0) notations.Returns
nullif the string isnull.- Parameters:
str – A
Stringto convert, may be null.- Returns:
converted
BigInteger(or null if the input is null).- Throws:
java.lang.NumberFormatException – Thrown if the value cannot be converted.
- Since:
3.2
- public static Double createDouble(String str)[source]
Creates a
Doublefrom aString.Returns
nullif the string isnull.
- public static Float createFloat(String str)[source]
Creates a
Floatfrom aString.Returns
nullif the string isnull.
- public static Integer createInteger(String str)[source]
Creates an
Integerfrom aString. Handles hexadecimal (0xhhhh) and octal (0dddd) notations. A leading zero means octal; spaces are not trimmed.Returns
nullif the string isnull.
- public static Long createLong(String str)[source]
Creates a
Longfrom aString. Handles hexadecimal (0Xhhhh) and octal (0ddd) notations. A leading zero means octal; spaces are not trimmed.Returns
nullif the string isnull.
- public static Number createNumber(String str)[source]
Creates a
Numberfrom aString.If the string starts with
0xor-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
IntegertoBigIntegerand fromFloattoBigDecimal.Integral values with a leading
0will be interpreted as octal; the returned number will be Integer, Long or BigDecimal as appropriate.Returns
nullif 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:
java.lang.NumberFormatException – Thrown if the value cannot be converted.
- public static boolean isCreatable(String str)[source]
Tests whether the String is a valid Java number.
Valid numbers include hexadecimal marked with the
0xor0Xqualifier, 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
09will returnfalse, since9is not a valid octal value. However, numbers beginning with0.are treated as decimal.nulland empty/blankStringwill returnfalse.Note,
#createNumber(String)should return a number for every input resulting intrue.- Parameters:
str – The
Stringto check.- Returns:
trueif the string is a correctly formatted number.- Since:
3.5
- public static boolean isDigits(String str)[source]
Tests whether the
Stringcontains only digit characters.nulland empty String will returnfalse.- Parameters:
str – The
Stringto check- Returns:
trueif str contains only Unicode numeric
- @Deprecated public static boolean isNumber(String str)[source]
Deprecated: This feature will be removed in Lang 4, use
NumberUtils#isCreatable(String)instead.Tests whether the String is a valid Java number.
Valid numbers include hexadecimal marked with the
0xor0Xqualifier, 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
09will returnfalse, since9is not a valid octal value. However, numbers beginning with0.are treated as decimal.nulland empty/blankStringwill returnfalse.Note,
#createNumber(String)should return a number for every input resulting intrue.- Parameters:
str – The
Stringto check.- Returns:
trueif the string is a correctly formatted number.- Since:
3.3 the code supports hexadecimal
0Xhhhan octal0dddvalidation.
- public static boolean isParsable(String str)[source]
Tests 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 catchingjava.text.ParseExceptionwhen calling one of those methods.Scientific notation (for example,
"1.2e-5") and type suffixes (e.g.,"2.0f","2.0d") are supported as they are valid forFloat#parseFloat(String)andDouble#parseDouble(String)as are"NaN","Infinity","+Infinity", and"-Infinity". Callers requiring finite-only validation should compose withDouble#isFinite(double).nulland empty String will returnfalse.- Parameters:
str – The String to check.
- Returns:
trueif the string is a parsable number.- See also:
- Since:
3.4
- public static byte max(byte a, byte b, byte c)[source]
Gets the maximum of three
bytevalues.- Parameters:
a – value 1.
b – value 2.
c – value 3.
- Returns:
The largest of the values.
- public static byte max(byte... array)[source]
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:
java.lang.NullPointerException – Thrown if
arrayisnull.java.lang.IllegalArgumentException – Thrown if
arrayis empty.
- Since:
3.4 Changed signature from max(byte[]) to max(byte…).
- public static double max(double a, double b, double c)[source]
Gets the maximum of three
doublevalues.If any value is
NaN,NaNis returned. Infinity is handled.- Parameters:
a – value 1.
b – value 2.
c – value 3.
- Returns:
The largest of the values.
- See also:
IEEE754rUtils#max(double, double, double)for a version of this method that handles NaN differently.
- public static double max(double... array)[source]
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:
java.lang.NullPointerException – Thrown if
arrayisnull.java.lang.IllegalArgumentException – Thrown if
arrayis empty.
- See also:
IEEE754rUtils#max(double[])IEEE754rUtils for a version of this method that handles NaN differently.- Since:
3.4 Changed signature from max(double[]) to max(double…)
- public static float max(float a, float b, float c)[source]
Gets the maximum of three
floatvalues.If any value is
NaN,NaNis returned. Infinity is handled.- Parameters:
a – value 1.
b – value 2.
c – value 3.
- Returns:
The largest of the values.
- See also:
IEEE754rUtils#max(float, float, float)for a version of this method that handles NaN differently.
- public static float max(float... array)[source]
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:
java.lang.NullPointerException – Thrown if
arrayisnull.java.lang.IllegalArgumentException – Thrown if
arrayis empty.
- See also:
IEEE754rUtils#max(float[])IEEE754rUtils for a version of this method that handles NaN differently.- Since:
3.4 Changed signature from max(float[]) to max(float…).
- public static int max(int a, int b, int c)[source]
Gets the maximum of three
intvalues.- Parameters:
a – value 1.
b – value 2.
c – value 3.
- Returns:
The largest of the values.
- public static int max(int... array)[source]
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:
java.lang.NullPointerException – Thrown if
arrayisnull.java.lang.IllegalArgumentException – Thrown if
arrayis empty.
- Since:
3.4 Changed signature from max(int[]) to max(int…).
- public static long max(long a, long b, long c)[source]
Gets the maximum of three
longvalues.- Parameters:
a – value 1.
b – value 2.
c – value 3.
- Returns:
The largest of the values.
- public static long max(long... array)[source]
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:
java.lang.NullPointerException – Thrown if
arrayisnull.java.lang.IllegalArgumentException – Thrown if
arrayis empty.
- Since:
3.4 Changed signature from max(long[]) to max(long…).
- public static short max(short a, short b, short c)[source]
Gets the maximum of three
shortvalues.- Parameters:
a – value 1.
b – value 2.
c – value 3.
- Returns:
The largest of the values.
- public static short max(short... array)[source]
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:
java.lang.NullPointerException – Thrown if
arrayisnull.java.lang.IllegalArgumentException – Thrown if
arrayis empty.
- Since:
3.4 Changed signature from max(short[]) to max(short…).
- public static byte min(byte a, byte b, byte c)[source]
Gets the minimum of three
bytevalues.- Parameters:
a – value 1.
b – value 2.
c – value 3.
- Returns:
The smallest of the values.
- public static byte min(byte... array)[source]
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:
java.lang.NullPointerException – Thrown if
arrayisnull.java.lang.IllegalArgumentException – Thrown if
arrayis empty.
- Since:
3.4 Changed signature from min(byte[]) to min(byte…).
- public static double min(double a, double b, double c)[source]
Gets the minimum of three
doublevalues.If any value is
NaN,NaNis returned. Infinity is handled.- Parameters:
a – value 1.
b – value 2.
c – value 3.
- Returns:
The smallest of the values.
- See also:
IEEE754rUtils#min(double, double, double)for a version of this method that handles NaN differently.
- public static double min(double... array)[source]
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:
java.lang.NullPointerException – Thrown if
arrayisnull.java.lang.IllegalArgumentException – Thrown if
arrayis empty.
- See also:
IEEE754rUtils#min(double[])IEEE754rUtils for a version of this method that handles NaN differently.- Since:
3.4 Changed signature from min(double[]) to min(double…).
- public static float min(float a, float b, float c)[source]
Gets the minimum of three
floatvalues.If any value is
NaN,NaNis returned. Infinity is handled.- Parameters:
a – value 1.
b – value 2.
c – value 3.
- Returns:
The smallest of the values.
- See also:
IEEE754rUtils#min(float, float, float)for a version of this method that handles NaN differently.
- public static float min(float... array)[source]
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:
java.lang.NullPointerException – Thrown if
arrayisnull.java.lang.IllegalArgumentException – Thrown if
arrayis empty.
- See also:
IEEE754rUtils#min(float[])IEEE754rUtils for a version of this method that handles NaN differently.- Since:
3.4 Changed signature from min(float[]) to min(float…).
- public static int min(int a, int b, int c)[source]
Gets the minimum of three
intvalues.- Parameters:
a – value 1.
b – value 2.
c – value 3.
- Returns:
The smallest of the values.
- public static int min(int... array)[source]
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:
java.lang.NullPointerException – Thrown if
arrayisnull.java.lang.IllegalArgumentException – Thrown if
arrayis empty.
- Since:
3.4 Changed signature from min(int[]) to min(int…).
- public static long min(long a, long b, long c)[source]
Gets the minimum of three
longvalues.- Parameters:
a – value 1.
b – value 2.
c – value 3.
- Returns:
The smallest of the values.
- public static long min(long... array)[source]
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:
java.lang.NullPointerException – Thrown if
arrayisnull.java.lang.IllegalArgumentException – Thrown if
arrayis empty.
- Since:
3.4 Changed signature from min(long[]) to min(long…).
- public static short min(short a, short b, short c)[source]
Gets the minimum of three
shortvalues.- Parameters:
a – value 1.
b – value 2.
c – value 3.
- Returns:
The smallest of the values.
- public static short min(short... array)[source]
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:
java.lang.NullPointerException – Thrown if
arrayisnull.java.lang.IllegalArgumentException – Thrown if
arrayis empty.
- Since:
3.4 Changed signature from min(short[]) to min(short…).
- public static byte toByte(String str)[source]
Converts a
Stringto abyte, returningzeroif the conversion fails.If the string is
null,zerois 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
zeroif conversion fails.- Since:
2.5
- public static byte toByte(String str, byte defaultValue)[source]
Converts a
Stringto 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 null.
defaultValue – The default value.
- Returns:
The byte represented by the string, or the default if conversion fails.
- Since:
2.5
- public static double toDouble(String str)[source]
Converts a
Stringto adouble, returning0.0dif the conversion fails.If the string
strisnull,0.0dis returned.NumberUtils.toDouble(null) = 0.0d NumberUtils.toDouble("") = 0.0d NumberUtils.toDouble("1.5") = 1.5d
- Parameters:
str – The string to convert, may be
null.- Returns:
The double represented by the string, or
0.0dif conversion fails.- Since:
2.1
- public static double toDouble(String str, double defaultValue)[source]
Converts a
Stringto adouble, returning a default value if the conversion fails.If the string
strisnull, 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 be
nulldefaultValue – The default value.
- Returns:
The double represented by the string, or defaultValue if conversion fails.
- Since:
2.1
- public static double toDouble(BigDecimal value)[source]
Converts a
BigDecimalto adouble.If the
BigDecimalvalueisnull, then the specified default value is returned.NumberUtils.toDouble(null) = 0.0d NumberUtils.toDouble(BigDecimal.valueOf(8.5d)) = 8.5d
- Parameters:
value – The
BigDecimalto convert, may benull.- Returns:
The double represented by the
BigDecimalor0.0dif theBigDecimalisnull.- Since:
3.8
- public static double toDouble(BigDecimal value, double defaultValue)[source]
Converts a
BigDecimalto adouble.If the
BigDecimalvalueisnull, 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 – The
BigDecimalto convert, may benull.defaultValue – The default value.
- Returns:
The double represented by the
BigDecimalor the defaultValue if theBigDecimalisnull.- Since:
3.8
- public static float toFloat(String str)[source]
Converts a
Stringto afloat, returning0.0fif the conversion fails.If the string
strisnull,0.0fis returned.NumberUtils.toFloat(null) = 0.0f NumberUtils.toFloat("") = 0.0f NumberUtils.toFloat("1.5") = 1.5f
- Parameters:
str – The string to convert, may be
null.- Returns:
The float represented by the string, or
0.0fif conversion fails.- Since:
2.1
- public static float toFloat(String str, float defaultValue)[source]
Converts a
Stringto afloat, returning a default value if the conversion fails.If the string
strisnull, 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 be
null.defaultValue – The default value.
- Returns:
The float represented by the string, or defaultValue if conversion fails.
- Since:
2.1
- public static int toInt(String str)[source]
Converts a
Stringto anint, returningzeroif the conversion fails.If the string is
null,zerois 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
zeroif conversion fails.- Since:
2.1
- public static int toInt(String str, int defaultValue)[source]
Converts a
Stringto 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 null.
defaultValue – The default value.
- Returns:
The int represented by the string, or the default if conversion fails.
- Since:
2.1
- public static long toLong(String str)[source]
Converts a
Stringto along, returningzeroif the conversion fails.If the string is
null,zerois 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
0if conversion fails.- Since:
2.1
- public static long toLong(String str, long defaultValue)[source]
Converts a
Stringto 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 null.
defaultValue – The default value.
- Returns:
The long represented by the string, or the default if conversion fails.
- Since:
2.1
- public static BigDecimal toScaledBigDecimal(Double value)[source]
Converts a
Doubleto aBigDecimalwith a scale of two that has been rounded usingRoundingMode.HALF_EVEN. If the suppliedvalueis null, thenBigDecimal.ZEROis returned.Note, the scale of a
BigDecimalis the number of digits to the right of the decimal point.- Parameters:
value – The
Doubleto convert, may be null.- Returns:
The scaled, with appropriate rounding,
BigDecimal.- Since:
3.8
- public static BigDecimal toScaledBigDecimal(Double value, int scale, RoundingMode roundingMode)[source]
Converts a
Doubleto aBigDecimalwhose scale is the specified value with aRoundingModeapplied. If the inputvalueisnull, we simply returnBigDecimal.ZERO.- Parameters:
value – The
Doubleto 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
- public static BigDecimal toScaledBigDecimal(Float value)[source]
Converts a
Floatto aBigDecimalwith a scale of two that has been rounded usingRoundingMode.HALF_EVEN. If the suppliedvalueis null, thenBigDecimal.ZEROis returned.Note, the scale of a
BigDecimalis the number of digits to the right of the decimal point.- Parameters:
value – The
Floatto convert, may be null.- Returns:
The scaled, with appropriate rounding,
BigDecimal.- Since:
3.8
- public static BigDecimal toScaledBigDecimal(Float value, int scale, RoundingMode roundingMode)[source]
Converts a
Floatto aBigDecimalwhose scale is the specified value with aRoundingModeapplied. If the inputvalueisnull, we simply returnBigDecimal.ZERO.- Parameters:
value – The
Floatto 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
- public static BigDecimal toScaledBigDecimal(String value)[source]
Converts a
Stringto aBigDecimalwith a scale of two that has been rounded usingRoundingMode.HALF_EVEN. If the suppliedvalueis null, thenBigDecimal.ZEROis returned.Note, the scale of a
BigDecimalis the number of digits to the right of the decimal point.- Parameters:
value – The
Stringto convert, may be null.- Returns:
The scaled, with appropriate rounding,
BigDecimal.- Since:
3.8
- public static BigDecimal toScaledBigDecimal(String value, int scale, RoundingMode roundingMode)[source]
Converts a
Stringto aBigDecimalwhose scale is the specified value with aRoundingModeapplied. If the inputvalueisnull, we simply returnBigDecimal.ZERO.- Parameters:
value – The
Stringto 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
- public static BigDecimal toScaledBigDecimal(BigDecimal value)[source]
Converts a
BigDecimalto aBigDecimalwith a scale of two that has been rounded usingRoundingMode.HALF_EVEN. If the suppliedvalueis null, thenBigDecimal.ZEROis returned.Note, the scale of a
BigDecimalis the number of digits to the right of the decimal point.- Parameters:
value – The
BigDecimalto convert, may be null.- Returns:
The scaled, with appropriate rounding,
BigDecimal.- Since:
3.8
- public static BigDecimal toScaledBigDecimal(BigDecimal value, int scale, RoundingMode roundingMode)[source]
Converts a
BigDecimalto aBigDecimalwhose scale is the specified value with aRoundingModeapplied. If the inputvalueisnull, we simply returnBigDecimal.ZERO.- Parameters:
value – The
BigDecimalto 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
- public static short toShort(String str)[source]
Converts a
Stringto ashort, returningzeroif the conversion fails.If the string is
null,zerois 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
zeroif conversion fails.- Since:
2.5
- public static short toShort(String str, short defaultValue)[source]
Converts a
Stringto 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 null.
defaultValue – The default value.
- Returns:
The short represented by the string, or the default if conversion fails.
- Since:
2.5