posted 12 years ago

Hi,

What is the standard way to round a double at a precision of x? I know there is a way when you multiply your double with a power of 10, then do a cast to int and then divide with the same power of 10.

Con of this method: when your double is bigger than the maximum value for integer, you will get always the maximum integer value, so that isn't a really good work around.

What is the standard way to round a double at a precision of x? I know there is a way when you multiply your double with a power of 10, then do a cast to int and then divide with the same power of 10.

Con of this method: when your double is bigger than the maximum value for integer, you will get always the maximum integer value, so that isn't a really good work around.

Tom Blough

Ranch Hand

Posts: 263

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 12 years ago

See if BigDecimal.round() does what you need. And remember floating types only approximate decimal amounts. For money or other critical decimal math you'll probably be happier with integral types or BigDecimal.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |