When working with ResultSet getDouble(), I want to get the correct value from the database. The value stored in the database is 11.421351.when I do resultSet.getDouble("lon").I get 11.4213599999 like that. How would I get this to just print 11.421351?
Asked
Active
Viewed 738 times
1
-
This may because doubles are stored like that(in java). You may try `getString()`. – dan1st May 21 '20 at 06:24
-
@dan1st That's not the problem here. – Tim Biegeleisen May 21 '20 at 06:27
-
1[Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Scary Wombat May 21 '20 at 06:28
1 Answers
1
The double
and float
types in Java use floating point arithmetic, and are not exact. Instead, you should be using exact types:
- In Java, use
BigDecimal
- In your SQL database, use
NUMERIC
orDECIMAL
Then, when you are accessing your result set from Java, use ResultSet#getBigDecimal
to obtain the number with its full original precision.

Tim Biegeleisen
- 502,043
- 27
- 286
- 360