![]() |
读研的葫芦 · 请回答1988 20集_哔哩哔哩_bilibili· 5 月前 · |
![]() |
豪爽的麦片 · 深化减贫合作,促进共同发展,推动全球发展倡议 ...· 7 月前 · |
![]() |
追风的打火机 · 国羽奥运巡礼:混双争金把握大 ...· 7 月前 · |
![]() |
满身肌肉的包子 · 第九版医学教材网盘资源下载 - 学霸盘· 8 月前 · |
![]() |
腼腆的松球 · 《古董局中局》众角色身份成谜 ...· 8 月前 · |
public class NumberUtils extends Object
Provides extra functionality for Java Number classes.
static
BigDecimal
toScaledBigDecimal
(
BigDecimal
value)
BigDecimal
to a
BigDecimal
with a scale of
two that has been rounded using
RoundingMode.HALF_EVEN
.
static
BigDecimal
toScaledBigDecimal
(
BigDecimal
value,
int scale,
RoundingMode
roundingMode)
BigDecimal
to a
BigDecimal
whose scale is the
specified value with a
RoundingMode
applied.
static
BigDecimal
toScaledBigDecimal
(
Double
value)
Double
to a
BigDecimal
with a scale of
two that has been rounded using
RoundingMode.HALF_EVEN
.
static
BigDecimal
toScaledBigDecimal
(
Double
value,
int scale,
RoundingMode
roundingMode)
Double
to a
BigDecimal
whose scale is the
specified value with a
RoundingMode
applied.
static
BigDecimal
toScaledBigDecimal
(
Float
value)
Float
to a
BigDecimal
with a scale of
two that has been rounded using
RoundingMode.HALF_EVEN
.
static
BigDecimal
toScaledBigDecimal
(
Float
value,
int scale,
RoundingMode
roundingMode)
Float
to a
BigDecimal
whose scale is the
specified value with a
RoundingMode
applied.
static
BigDecimal
toScaledBigDecimal
(
String
value)
String
to a
BigDecimal
with a scale of
two that has been rounded using
RoundingMode.HALF_EVEN
.
static
BigDecimal
toScaledBigDecimal
(
String
value,
int scale,
RoundingMode
roundingMode)
String
to a
BigDecimal
whose scale is the
specified value with a
RoundingMode
applied.
static short
toShort
(
String
str)
String
to a
short
, returning
zero
if the conversion fails.
static short
toShort
(
String
str,
short defaultValue)
String
to an
short
, returning a
default value if the conversion fails.
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.
str
- the string to convert, may be null
zero
if
conversion fails
public 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.
str
- the string to convert, may be null
defaultValue
- the default value
public 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.
str
- the string to convert, may be null
0
if
conversion fails
public 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.
str
- the string to convert, may be null
defaultValue
- the default value
public 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.
str
- the string to convert, may be
null
0.0f
if conversion fails
public 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.
str
- the string to convert, may be
null
defaultValue
- the default value
public 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.
str
- the string to convert, may be
null
0.0d
if conversion fails
public 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.
str
- the string to convert, may be
null
defaultValue
- the default value
public static double toDouble(BigDecimal value)
Convert a
BigDecimal
to a
double
.
If the
BigDecimal
value
is
null
, then the specified default value is returned.
value
- the
BigDecimal
to convert, may be
null
.
BigDecimal
or
0.0d
if the
BigDecimal
is
null
.
public static double toDouble(BigDecimal value, double defaultValue)
Convert a
BigDecimal
to a
double
.
If the
BigDecimal
value
is
null
, then the specified default value is returned.
value
- the
BigDecimal
to convert, may be
null
.
defaultValue
- the default value
BigDecimal
or the
defaultValue if the
BigDecimal
is
null
.
public 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.
str
- the string to convert, may be null
zero
if
conversion fails
public 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.
str
- the string to convert, may be null
defaultValue
- the default value
public 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.
str
- the string to convert, may be null
zero
if
conversion fails
public 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.
str
- the string to convert, may be null
defaultValue
- the default value
public static BigDecimal toScaledBigDecimal(BigDecimal value)
BigDecimal
to a
BigDecimal
with a scale of
two that has been rounded using
RoundingMode.HALF_EVEN
. If the supplied
value
is null, then
BigDecimal.ZERO
is returned.
Note, the scale of a
BigDecimal
is the number of digits to the right of the
decimal point.
value
- the
BigDecimal
to convert, may be null.
BigDecimal
.
public static BigDecimal toScaledBigDecimal(BigDecimal value, int scale, RoundingMode roundingMode)
BigDecimal
to a
BigDecimal
whose scale is the
specified value with a
RoundingMode
applied. If the input
value
is
null
, we simply return
BigDecimal.ZERO
.
value
- the
BigDecimal
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.
BigDecimal
.
public static BigDecimal toScaledBigDecimal(Float value)
Float
to a
BigDecimal
with a scale of
two that has been rounded using
RoundingMode.HALF_EVEN
. If the supplied
value
is null, then
BigDecimal.ZERO
is returned.
Note, the scale of a
BigDecimal
is the number of digits to the right of the
decimal point.
value
- the
Float
to convert, may be null.
BigDecimal
.
public static BigDecimal toScaledBigDecimal(Float value, int scale, RoundingMode roundingMode)
Float
to a
BigDecimal
whose scale is the
specified value with a
RoundingMode
applied. If the input
value
is
null
, we simply return
BigDecimal.ZERO
.
value
- the
Float
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.
BigDecimal
.
public static BigDecimal toScaledBigDecimal(Double value)
Double
to a
BigDecimal
with a scale of
two that has been rounded using
RoundingMode.HALF_EVEN
. If the supplied
value
is null, then
BigDecimal.ZERO
is returned.
Note, the scale of a
BigDecimal
is the number of digits to the right of the
decimal point.
value
- the
Double
to convert, may be null.
BigDecimal
.
public static BigDecimal toScaledBigDecimal(Double value, int scale, RoundingMode roundingMode)
Double
to a
BigDecimal
whose scale is the
specified value with a
RoundingMode
applied. If the input
value
is
null
, we simply return
BigDecimal.ZERO
.
value
- the
Double
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.
BigDecimal
.
public static BigDecimal toScaledBigDecimal(String value)
String
to a
BigDecimal
with a scale of
two that has been rounded using
RoundingMode.HALF_EVEN
. If the supplied
value
is null, then
BigDecimal.ZERO
is returned.
Note, the scale of a
BigDecimal
is the number of digits to the right of the
decimal point.
value
- the
String
to convert, may be null.
BigDecimal
.
public static BigDecimal toScaledBigDecimal(String value, int scale, RoundingMode roundingMode)
String
to a
BigDecimal
whose scale is the
specified value with a
RoundingMode
applied. If the input
value
is
null
, we simply return
BigDecimal.ZERO
.
value
- the
String
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.
BigDecimal
.
public static Number createNumber(String str)
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
.
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 null
NumberFormatException
- if the value cannot be converted
public 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 null
Float
(or null if the input is null)
NumberFormatException
- if the value cannot be converted
public 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 null
Double
(or null if the input is null)
NumberFormatException
- if the value cannot be converted
public 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 null
Integer
(or null if the input is null)
NumberFormatException
- if the value cannot be converted
public 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 null
Long
(or null if the input is null)
NumberFormatException
- if the value cannot be converted
public 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 null
BigInteger
(or null if the input is null)
NumberFormatException
- if the value cannot be converted
public 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 null
BigDecimal
(or null if the input is null)
NumberFormatException
- if the value cannot be converted
IllegalArgumentException
- if
array
is
null
IllegalArgumentException
- if
array
is empty
IllegalArgumentException
- if
array
is
null
IllegalArgumentException
- if
array
is empty
IllegalArgumentException
- if
array
is
null
IllegalArgumentException
- if
array
is empty
IllegalArgumentException
- if
array
is
null
IllegalArgumentException
- if
array
is empty
IllegalArgumentException
- if
array
is
null
IllegalArgumentException
- if
array
is empty
IEEE754rUtils for a version of this method that handles NaN differently
IllegalArgumentException
- if
array
is
null
IllegalArgumentException
- if
array
is empty
IEEE754rUtils for a version of this method that handles NaN differently
IllegalArgumentException
- if
array
is
null
IllegalArgumentException
- if
array
is empty
IllegalArgumentException
- if
array
is
null
IllegalArgumentException
- if
array
is empty
IllegalArgumentException
- if
array
is
null
IllegalArgumentException
- if
array
is empty
IllegalArgumentException
- if
array
is
null
IllegalArgumentException
- if
array
is empty
IllegalArgumentException
- if
array
is
null
IllegalArgumentException
- if
array
is empty
IEEE754rUtils for a version of this method that handles NaN differently
IllegalArgumentException
- if
array
is
null
IllegalArgumentException
- if
array
is empty
IEEE754rUtils for 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 check
true
if str contains only Unicode numeric
@Deprecated public static boolean isNumber(String str)
isCreatable(String)
instead
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
.
Note,
createNumber(String)
should return a number for every
input resulting in
true
.
str
- the
String
to check
true
if the string is a correctly formatted number
0Xhhh
an
octal
0ddd
validation
public static boolean isCreatable(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
.
Note,
createNumber(String)
should return a number for every
input resulting in
true
.
str
- the
String
to check
true
if the string is a correctly formatted number
public 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
isCreatable(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 compare
y
- the second
int
to compare
0
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 compare
y
- the second
long
to compare
0
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 compare
y
- the second
short
to compare
0
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 compare
y
- the second
byte
to compare
0
if
x == y
;
a value less than
0
if
x < y
; and
a value greater than
0
if
x > y
![]() |
读研的葫芦 · 请回答1988 20集_哔哩哔哩_bilibili 5 月前 |
![]() |
追风的打火机 · 国羽奥运巡礼:混双争金把握大 女单女双亦有戏_陈雨菲 7 月前 |
![]() |
满身肌肉的包子 · 第九版医学教材网盘资源下载 - 学霸盘 8 月前 |