sun*_*985 2 java numberformatexception
我试图使用Java将字符串"1000000000000001"转换为基数5:
Integer number = Integer.parseInt("1000000000000001", 5);
但是,我是gettig NumberFormatException.该字符串被修剪,只包含1和0.有人可以解释为什么我得到这个例外?
数量1000000000000001
在基地5相当于数30517578126
以10为基数(你可以验证这一点自己或使用在线工具).
但是,30,517,578,126
太大而不适合int
价值.最大值Integer.MAX_VALUE
是2,147,483,647
.这解释了您获得的例外情况 - 来自parseInt
:
throws
NumberFormatException
- 如果String不包含可解析的int
.
这是这种情况.
你需要使用long
:
public static void main(String[] args) {
long number = Long.parseLong("1000000000000001", 5);
System.out.println(number); // prints "30517578126"
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
86 次 |
最近记录: |