public class Numbers extends Object
| 构造器和说明 |
|---|
Numbers() |
| 限定符和类型 | 方法和说明 |
|---|---|
static Number |
add(Number left,
Number right) |
static int |
compare(byte x,
byte y)
Compares two
byte values numerically. |
static int |
compare(char x,
char y)
Compares two
char values numerically. |
static int |
compare(double d1,
double d2)
Compares the two specified
double values. |
static int |
compare(float f1,
float f2)
Compares the two specified
float values. |
static int |
compare(int x,
int y)
Compares two
int values numerically. |
static int |
compare(long x,
long y)
Compares two
long values numerically. |
static int |
compare(short x,
short y)
Compares two
short values numerically. |
static <T extends Number> |
convertNumberToTargetClass(Number number,
Class<T> targetClass)
Convert the given number into an instance of the given target class.
|
static BigDecimal |
createBigDecimal(String str)
Convert a
String to a BigDecimal. |
static BigInteger |
createBigInteger(String str)
Convert a
String to a BigInteger. |
static Byte |
createByte(String str) |
static Double |
createDouble(Object d) |
static Double |
createDouble(String str)
Convert a
String to a Double. |
static Float |
createFloat(Object d) |
static Float |
createFloat(String str)
Convert a
String to a Float. |
static Integer |
createInteger(Object d) |
static Integer |
createInteger(String str) |
static Long |
createLong(Object d) |
static Long |
createLong(String str)
Convert a
String to a Long. |
static Number |
createNumber(String str) |
static Short |
createShort(String str) |
static Number |
div(Number left,
Number right) |
static boolean |
isDigits(String str)
Checks whether the
String contains only
digit characters. |
static boolean |
isDouble(Number number) |
static boolean |
isInteger(Number number) |
static boolean |
isLong(Number number) |
static boolean |
isNumber(String str)
Checks whether the String a valid Java number.
|
static boolean |
isZero(Number number)
判断是否为0
|
static Number |
mod(Number left,
Number right) |
static Number |
mul(Number left,
Number right) |
static <T extends Number> |
parseNumber(String text,
Class<T> targetClass)
Parse the given
text into a Number instance of the given
target class, using the corresponding decode / valueOf method. |
static <T extends Number> |
parseNumber(String text,
Class<T> targetClass,
NumberFormat numberFormat)
Parse the given
text into a Number instance of the
given target class, using the supplied NumberFormat. |
static int |
parseUnsignedInt(String s)
Parses the string argument as an unsigned decimal integer.
|
static int |
parseUnsignedInt(String s,
int radix) |
static Number |
sub(Number left,
Number right) |
static BigDecimal |
toBigDecimal(Number number)
|
static byte |
toByte(Number number) |
static double |
toDouble(Number number) |
static float |
toFloat(Number number) |
static int |
toInt(Number number) |
static long |
toLong(Number number) |
static short |
toShort(Number number) |
public static int parseUnsignedInt(String s) throws NumberFormatException
'+' ('\u002B'). The resulting integer value
is returned, exactly as if the argument and the radix 10 were
given as arguments to the parseUnsignedInt(java.lang.String, int) method.s - a String containing the unsigned int
representation to be parsedNumberFormatException - if the string does not contain a
parsable unsigned integer.public static int parseUnsignedInt(String s, int radix) throws NumberFormatException
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 nullFloatNumberFormatException - 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 nullDoubleNumberFormatException - if the value cannot be convertedpublic static Long createLong(String str)
Convert a String to a Long.
Returns null if the string is null.
str - a String to convert, may be nullLongNumberFormatException - if the value cannot be convertedpublic static BigInteger createBigInteger(String str)
Convert a String to a BigInteger.
Returns null if the string is null.
str - a String to convert, may be nullBigIntegerNumberFormatException - 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 nullBigDecimalNumberFormatException - if the value cannot be convertedpublic static BigDecimal toBigDecimal(Number number)
number - 数字BigDecimalpublic static boolean isZero(Number number)
number - 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
qualifier, scientific notation and numbers marked with a type
qualifier (e.g. 123L).
Null and empty String will return
false.
str - the String to checktrue if the string is a correctly formatted numberpublic static Number createNumber(String str) throws NumberFormatException
public static <T extends Number> T parseNumber(String text, Class<T> targetClass)
text into a Number instance of the given
target class, using the corresponding decode / valueOf method.
Trims all whitespace (leading, trailing, and in between characters) from
the input String before attempting to parse the number.
Supports numbers in hex format (with leading "0x", "0X", or "#") as well.
text - the text to converttargetClass - the target class to parse intoIllegalArgumentException - if the target class is not supported
(i.e. not a standard Number subclass as included in the JDK)Byte.decode(java.lang.String),
Short.decode(java.lang.String),
Integer.decode(java.lang.String),
Long.decode(java.lang.String),
decodeBigInteger(String),
createFloat(String),
createDouble(String),
BigDecimal.BigDecimal(String)public static <T extends Number> T parseNumber(String text, Class<T> targetClass, @Nullable NumberFormat numberFormat)
text into a Number instance of the
given target class, using the supplied NumberFormat.
Trims the input String before attempting to parse the number.
text - the text to converttargetClass - the target class to parse intonumberFormat - the NumberFormat to use for parsing (if
null, this method falls back to parseNumber(String, Class))IllegalArgumentException - if the target class is not supported
(i.e. not a standard Number subclass as included in the JDK)NumberFormat.parse(java.lang.String, java.text.ParsePosition),
convertNumberToTargetClass(java.lang.Number, java.lang.Class<T>),
parseNumber(String, Class)public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) throws IllegalArgumentException
number - the number to converttargetClass - the target class to convert toIllegalArgumentException - if the target class is not supported
(i.e. not a standard Number subclass as included in the JDK)Byte,
Short,
Integer,
Long,
BigInteger,
Float,
Double,
BigDecimalpublic static int toInt(Number number)
public static long toLong(Number number)
public static double toDouble(Number number)
public static float toFloat(Number number)
public static short toShort(Number number)
public static byte toByte(Number number)
public static boolean isInteger(Number number)
public static boolean isDouble(Number number)
public static boolean isLong(Number number)
public static int compare(long x,
long y)
long values numerically.
The value returned is identical to what would be returned by:
Long.valueOf(x).compareTo(Long.valueOf(y))
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 > ypublic static int compare(int x,
int y)
int values numerically.
The value returned is identical to what would be returned by:
Integer.valueOf(x).compareTo(Integer.valueOf(y))
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 > ypublic static int compare(byte x,
byte y)
byte values numerically.
The value returned is identical to what would be returned by:
Byte.valueOf(x).compareTo(Byte.valueOf(y))
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 > ypublic static int compare(char x,
char y)
char values numerically.
The value returned is identical to what would be returned by:
Character.valueOf(x).compareTo(Character.valueOf(y))
x - the first char to comparey - the second char to compare0 if x == y;
a value less than 0 if x < y; and
a value greater than 0 if x > ypublic static int compare(short x,
short y)
short values numerically.
The value returned is identical to what would be returned by:
Short.valueOf(x).compareTo(Short.valueOf(y))
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 > ypublic static int compare(float f1,
float f2)
float values. The sign
of the integer value returned is the same as that of the
integer that would be returned by the call:
new Float(f1).compareTo(new Float(f2))
f1 - the first float to compare.f2 - the second float to compare.0 if f1 is
numerically equal to f2; a value less than
0 if f1 is numerically less than
f2; and a value greater than 0
if f1 is numerically greater than
f2.public static int compare(double d1,
double d2)
double values. The sign
of the integer value returned is the same as that of the
integer that would be returned by the call:
new Double(d1).compareTo(new Double(d2))
d1 - the first double to compared2 - the second double to compare0 if d1 is
numerically equal to d2; a value less than
0 if d1 is numerically less than
d2; and a value greater than 0
if d1 is numerically greater than
d2.Copyright © 2022. All rights reserved.