Set specific precision of a BigDecimal

Jason picture Jason · Feb 28, 2012 · Viewed 102.8k times · Source

I have an XSD that requires me to use a BigDecimal for a lat/lon. I currently have the lat/lon as doubles, and convert them to BigDecimal, but I am only required to use about 12 places of precision. I have not been able to figure out how to set that. Can anyone help me with this?

Answer

Peter Lawrey picture Peter Lawrey · Feb 28, 2012

You can use setScale() e.g.

double d = ...
BigDecimal db = new BigDecimal(d).setScale(12, BigDecimal.ROUND_HALF_UP);