public class NumberParser extends Object
构造时可选是否将NaN转为0,默认为true。
参考:https://stackoverflow.com/questions/5876369/why-does-casting-double-nan-to-int-not-throw-an-exception-in-java
Modifier and Type | Field and Description |
---|---|
static NumberParser |
INSTANCE
单例
|
Constructor and Description |
---|
NumberParser(Locale locale,
boolean zeroIfNaN)
构造
|
Modifier and Type | Method and Description |
---|---|
static NumberParser |
of(Locale locale)
构建NumberParser
|
static NumberParser |
of(Locale locale,
boolean zeroIfNaN)
构建NumberParser
|
BigInteger |
parseBigInteger(String str)
解析为
BigInteger ,支持16进制、10进制和8进制,如果传入空白串返回nullfrom Apache Common Lang |
double |
parseDouble(String numberStr)
解析转换数字字符串为long型数字,规则如下:
1、0开头的忽略开头的0
2、空串返回0
3、其它情况按照10进制转换
4、.123形式返回0.123(按照小于0的小数对待)
5、NaN返回0
|
float |
parseFloat(String numberStr)
解析转换数字字符串为long型数字,规则如下:
1、0开头的忽略开头的0
2、空串返回0
3、其它情况按照10进制转换
4、.123形式返回0.123(按照小于0的小数对待)
|
int |
parseInt(char[] chars,
int radix)
转换char数组为一个int值,此方法拷贝自
Integer.parseInt(String, int) 拷贝的原因是直接转换char[]避免创建String对象造成的多余拷贝 此方法自动跳过首尾空白符 |
int |
parseInt(String numberStr)
解析转换数字字符串为int型数字,规则如下:
1、0x开头的视为16进制数字
2、0开头的忽略开头的0
3、其它情况按照10进制转换
4、空串返回0
5、.123形式返回0(按照小于0的小数对待)
6、123.56截取小数点之前的数字,忽略小数部分
7、科学计数法抛出NumberFormatException异常
|
long |
parseLong(String numberStr)
解析转换数字字符串为long型数字,规则如下:
1、0x开头的视为16进制数字
2、0开头的忽略开头的0
3、空串返回0
4、其它情况按照10进制转换
5、.123形式返回0(按照小于0的小数对待)
6、123.56截取小数点之前的数字,忽略小数部分
|
Number |
parseNumber(String numberStr)
将指定字符串转换为
Number 对象此方法不支持科学计数法 空白符和NaN转换为0 0x开头使用16进制解析为Long类型 需要注意的是,在不同Locale下,数字的表示形式也是不同的,例如: 德国、荷兰、比利时、丹麦、意大利、罗马尼亚和欧洲大多地区使用`,`区分小数 也就是说,在这些国家地区,1.20表示120,而非1.2。 |
public static final NumberParser INSTANCE
public NumberParser(Locale locale, boolean zeroIfNaN)
locale
- 地域,null
表示默认本地zeroIfNaN
- 如果用户传入NaN,是否转为0,如果为false
,则抛出NumberFormatException
public static NumberParser of(Locale locale)
locale
- 地域,null
表示默认本地public static NumberParser of(Locale locale, boolean zeroIfNaN)
locale
- 地域,null
表示默认本地zeroIfNaN
- 如果用户传入NaN,是否转为0,如果为false
,则抛出NumberFormatException
public int parseInt(String numberStr) throws NumberFormatException
1、0x开头的视为16进制数字 2、0开头的忽略开头的0 3、其它情况按照10进制转换 4、空串返回0 5、.123形式返回0(按照小于0的小数对待) 6、123.56截取小数点之前的数字,忽略小数部分 7、科学计数法抛出NumberFormatException异常
numberStr
- 数字字符串NumberFormatException
- 数字格式异常public int parseInt(char[] chars, int radix)
Integer.parseInt(String, int)
chars
- char数组radix
- 进制数Integer.parseInt(String, int)
public long parseLong(String numberStr)
1、0x开头的视为16进制数字 2、0开头的忽略开头的0 3、空串返回0 4、其它情况按照10进制转换 5、.123形式返回0(按照小于0的小数对待) 6、123.56截取小数点之前的数字,忽略小数部分
numberStr
- 数字字符串public float parseFloat(String numberStr)
1、0开头的忽略开头的0 2、空串返回0 3、其它情况按照10进制转换 4、.123形式返回0.123(按照小于0的小数对待)
numberStr
- 数字字符串public double parseDouble(String numberStr)
1、0开头的忽略开头的0 2、空串返回0 3、其它情况按照10进制转换 4、.123形式返回0.123(按照小于0的小数对待) 5、NaN返回0
numberStr
- 数字字符串public BigInteger parseBigInteger(String str)
BigInteger
,支持16进制、10进制和8进制,如果传入空白串返回nullstr
- 数字字符串BigInteger
public Number parseNumber(String numberStr) throws NumberFormatException
Number
对象
需要注意的是,在不同Locale下,数字的表示形式也是不同的,例如:
德国、荷兰、比利时、丹麦、意大利、罗马尼亚和欧洲大多地区使用`,`区分小数
也就是说,在这些国家地区,1.20表示120,而非1.2。
numberStr
- 数字字符串NumberFormatException
- 包装了ParseException
,当给定的数字字符串无法解析时抛出Copyright © 2025. All rights reserved.