I've spend a long time now, trying to convert the number 1.2846202978398e+19 in java, without any luck. Currently what I'm trying to do (long)Double.parseDouble(hashes)
, however this gives 9223372036854775807, which is obviously incorrect. The actual number should look something like this 12855103593745000000.
Using int val = new BigDecimal(stringValue).intValue();
returns -134589568
as it's unable to hold the result. Switching the code to long val = new BigDecimal(hashes).longValue();
gives me -5600541095311551616 which is also incorrect.
I'm assuming this is happening due to the size of a double compared to a long.
Any ideas?
Did you try to use String.format
:
String result = String.format("%.0f", Double.parseDouble("1.2846202978398e+19"));
System.out.println(result);
Output
12846202978398000000
Edit
Why you don't work with BigDecimal
to do the arithmetic operations, for example :
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)));
Output
128462029783980000000
12846202978398000000