Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

java - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can also just use BigDecimal notation:

?-71.0712654000000200000g?.setScale(16) or ?-71.07126540000002000000G.setScale(16)

Both result in:

-71.0712654000000200

Another option is setting a custom MathContext.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...