Rounding off a number
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sandeep_khandelwal
Posted On:   Tuesday, June 12, 2001 12:10 AM

How do I round off a decimal number stored in double so that 12.80 becomes 13 and 12.20 becomes 12.

And if double returns me a value 12.2345456654 then rounding it off to two decimal places, how is it done?

Re: Rounding off a number

Posted By:   Anonymous  
Posted On:   Tuesday, June 12, 2001 02:57 AM

Hi,

Use class BigDecimal.


BigDecimal bd = new BigDecimal(12.2345456654);


bd = bd.setScale(2,BigDecimal.ROUND_HALF_DOWN);


System.out.println(bd.doubleValue());
//rounds to 2 decimal places
About | Sitemap | Contact