String to double with precision

I do have a String that is read from the file with the value: 38.739793110376837

When i convert this value to double using:

double.parseDouble("38.739793110376837");

I get the result: 38.73979311037684

How can i have the original value in a double variable?

I don't want to use BigDecimal data type.

Jon Skeet
people
quotationmark

How can i have the original value in a double variable?

You can't. The closest double to your original value is exactly 38.739793110376837148578488267958164215087890625

That's being converted to "38.73979311037684" because that's the shortest value which uniquely identifies that double value. From the documentation:

How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument d. Then d must be the double value nearest to x; or if two double values are equally close to x, then d must be one of them and the least significant bit of the significand of d must be 0.

If you want to retain exactly your original value, you should be using BigDecimal.

people

See more on this question at Stackoverflow