How do I convert a String number: "6542.5699999999997" into a long? Lets say I want to have 8 digit precision in the long so it should look like:
long: 654257000000
And no I don't want to use a BigInt.
You can first multiply, then round like so:
long val = (long)(6542.5699999999997 * 100000000 + 0.5);
If the number can also be negative, you have to handle that case separately because the + 0.5
trick works only for positive values.
Try this:
NumberFormat formatter = new DecimalFormat("#0.00000000");
String longStr = formatter.format(6542.5699999999997).replaceAll("\\D", "");
Long longVal = Long.parseLong(longStr);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.