Jaz*_*rix 3 java scientific-notation long-integer
我现在花了很长时间,尝试在java中转换数字1.2846202978398e+19,但没有任何运气。目前我正在尝试做的事情(long)Double.parseDouble(hashes)
,但这给出了 9223372036854775807,这显然是不正确的。实际数字应类似于 12855103593745000000。
使用int val = new BigDecimal(stringValue).intValue();
return-134589568
因为它无法保存结果。将代码切换为long val = new BigDecimal(hashes).longValue();
-5600541095311551616 这也是不正确的。
我假设这是由于双精度型与长型相比的大小造成的。
有任何想法吗?
您是否尝试使用String.format
:
String result = String.format("%.0f", Double.parseDouble("1.2846202978398e+19"));
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)
输出
12846202978398000000
Run Code Online (Sandbox Code Playgroud)
编辑
为什么你不使用BigDecimal
来进行算术运算,例如:
String str = "1.2846202978398e+19";
BigDecimal d = new BigDecimal(str).multiply(BigDecimal.TEN);
// ^^^^^^^^------example of arithmetic operations
System.out.println(String.format("%.0f", d));
System.out.println(String.format("%.0f", Double.parseDouble(str)));
Run Code Online (Sandbox Code Playgroud)
输出
128462029783980000000
12846202978398000000
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8471 次 |
最近记录: |