Page 2 of 2

Re: Truncating Doubles

Posted: Thu Feb 23, 2023 1:53 pm
by AllanH
The core java math class also provides implementations that properly rounds, truncates, performs ceiling, and similar. For instance, one of the round methods are:

static long round(double a) -- Returns the closest long to the argument, with ties rounding to positive infinity.

https://docs.oracle.com/en/java/javase/ ... /Math.html

Re: Truncating Doubles

Posted: Fri Feb 24, 2023 2:43 am
by terrymcg
If you want direct control over the precision of floating point (double) calculations, you can use BigDecimal.

It's a bit awkward (and slower), but means there's less chance of results with unexpected rounding errors.

https://medium.com/@reachansari/use-of- ... 1440bdd5f4

VM has 'cents' too ;)

Cheers,
--
Terry McG