public class Numbers extends Object
| 构造器和说明 |
|---|
Numbers() |
| 限定符和类型 | 方法和说明 |
|---|---|
static Number |
add(Number left,
Number right) |
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 Double |
createDouble(String str)
Convert a
String to a Double. |
static Float |
createFloat(String str)
Convert a
String to a Float. |
static Integer |
createInteger(String str) |
static Long |
createLong(String str)
Convert a
String to a Long. |
static Number |
createNumber(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 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 Number |
sub(Number left,
Number right) |
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 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 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),
Float.valueOf(java.lang.String),
Double.valueOf(java.lang.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)
Copyright © 2020. All rights reserved.