Q:

Java How to represent a decimal number in a long

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.

A:

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.

