public class NumberKits extends Object
copy from vipshop VJTools(com.vip.vjtools.vjkit.number.NumberUtil) and made some changes.
| 构造器和说明 |
|---|
NumberKits() |
| 限定符和类型 | 方法和说明 |
|---|---|
static <T extends Number> |
convertNumberToTargetClass(Number number,
Class<T> targetClass)
将给定的数字转换为给定目标类的实例
|
static boolean |
equalsWithin(double d1,
double d2)
因为double的精度问题, 允许两个double在0.00001内的误差为相等。
|
static boolean |
equalsWithin(double d1,
double d2,
double epsilon)
因为double的精度问题, 允许两个double在epsilon内的误差为相等
|
static boolean |
isHexNumber(String value)
判断字符串是否16进制
|
static boolean |
isNumber(String str)
判断字符串是否合法数字
|
static <T extends Number> |
parseNumber(Object val,
Class<T> targetClass)
Parse the given
val into a Number instance of the given
target class, the default value is returned if an error occurs. |
static <T extends Number> |
parseNumber(Object val,
Class<T> targetClass,
T defaultValue)
Parse the given
val into a Number instance of the given
target class, the default value is returned if an error occurs. |
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 BigDecimal |
round(Number number,
int scale,
RoundingMode roundingMode)
保留固定位数小数
例如保留四位小数:123.456789 =》 123.4567 |
static BigDecimal |
round(String number,
int scale,
RoundingMode roundingMode)
保留固定位数小数
例如保留四位小数:123.456789 =》 123.4567 |
static String |
to2DigitString(double d)
输出格式化为小数后两位的double字符串
|
static byte[] |
toBytes(double val)
copy from ElasticSearch Numbers
|
static byte[] |
toBytes(int value) |
static byte[] |
toBytes(long value) |
static double |
toDouble(byte[] bytes)
copy from ElasticSearch Numbers
|
static int |
toInt(byte[] bytes) |
static int |
toInt32(long x)
安全的将小于Integer.MAX的long转为int,否则抛出IllegalArgumentException异常
|
static long |
toLong(byte[] bytes) |
static String |
toString(double d) |
static String |
toString(Double d) |
static String |
toString(float f) |
static String |
toString(Float f) |
static String |
toString(int i) |
static String |
toString(Integer i) |
static String |
toString(long l) |
static String |
toString(Long l) |
public static boolean equalsWithin(double d1,
double d2)
public static boolean equalsWithin(double d1,
double d2,
double epsilon)
public static boolean isNumber(String str)
public static boolean isHexNumber(String value)
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) throws IllegalArgumentException
number - 将要转换的数字targetClass - 目标类IllegalArgumentException - 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 <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 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(Object val, Class<T> targetClass, T defaultValue)
val into a Number instance of the given
target class, the default value is returned if an error occurs.
Trims the input Object before attempting to parse the number.
Supports numbers in hex format (with leading "0x", "0X", or "#") as well.
val - an object to converttargetClass - the target class to parse intodefaultValue - an default valueIllegalArgumentException - 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(Object val, Class<T> targetClass)
val into a Number instance of the given
target class, the default value is returned if an error occurs.
Trims the input Object before attempting to parse the number.
Supports numbers in hex format (with leading "0x", "0X", or "#") as well.
val - an object 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 String toString(int i)
public static String toString(float f)
public static String toString(long l)
public static String toString(double d)
public static String to2DigitString(double d)
public static BigDecimal round(Number number, int scale, RoundingMode roundingMode)
number - 数字值scale - 保留小数位数,如果传入小于0,则默认0roundingMode - 保留小数的模式 RoundingMode,如果传入null则默认四舍五入public static BigDecimal round(String number, int scale, RoundingMode roundingMode)
number - 数字值scale - 保留小数位数,如果传入小于0,则默认0roundingMode - 保留小数的模式 RoundingMode,如果传入null则默认四舍五入public static int toInt32(long x)
public static byte[] toBytes(int value)
public static byte[] toBytes(long value)
public static byte[] toBytes(double val)
public static int toInt(byte[] bytes)
public static long toLong(byte[] bytes)
public static double toDouble(byte[] bytes)
Copyright © 2019. All rights reserved.