Not Known Not Known - 1 month ago 5
Groovy Question

BigDecimal issue with long and lat

I have an address domain , which has fields lat and long as BigDecimal and i am using constraints of scale as 16. I have this address

550 Tremont St, Boston, MA 02116, USA


its long and lat are -71.07126540000002 and 42.3438919

Longitude for this address is stored in db as -71.0712654000000200. now i need to compare the saved longitude with any new request to check if longitude already exist.

I am sending the same longitude again -71.07126540000002 but i am not able to convert it the form as it saved in db (-71.0712654000000200) before comparing as they are the long. of same address.

i tried using

BigDecimal a = new BigDecimal(-71.07126540000002)
println a.setScale(16, RoundingMode.CEILING)​ // tried all other RoundingMode


but all are giving response as either -71.0712654000000156 or -71.0712654000000157 but not getting -71.0712654000000200

Help!

Answer

It should be used

a.setScale(14, RoundingMode.HALF_UP)

since after 14th pos. zeros are not significative

at the end will be -71.07126540000002

Comments