posted 13 years ago

Hi

Just a quick one. Anyone know how to restrict the number of decimal places in a doouble value to 2?

I've had a look around and seen a few answer with silly caluculations (divide by 100 * by itself etc...) but none of them seem to work. All i want to do is reduce the decimal places for a value

so for example: 10.714285714285714% becomes 10.71%

thanx

Just a quick one. Anyone know how to restrict the number of decimal places in a doouble value to 2?

I've had a look around and seen a few answer with silly caluculations (divide by 100 * by itself etc...) but none of them seem to work. All i want to do is reduce the decimal places for a value

so for example: 10.714285714285714% becomes 10.71%

thanx

Cheers!

posted 13 years ago

If what you want to do is to obtain a textual representation of the value, with two decimal places, DecimalFormat is indeed the way to go.

If what you want to do is to perform

If what you want to do is to perform

**calculations**using only two decimal places, then you should not be using floating-point arithmetic. Instead, you should do integer calculations, using "hundredths" (if you're doing currency, call them "cents", "pence" etc.).Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.

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