Truncating Doubles

AllanH
Posts: 13
Joined: Sat Nov 26, 2022 5:23 pm

Re: Truncating Doubles

Post 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
terrymcg
Posts: 85
Joined: Mon Apr 08, 2019 12:35 am

Re: Truncating Doubles

Post 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
Post Reply

Return to “Module Designer”