Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Rounding a Decimal to Two Decimal Places in Java
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SS1389




PostPosted: Sat Nov 20, 2010 9:45 pm   Post subject: Rounding a Decimal to Two Decimal Places in Java

Hey guys,

I was just wondering what's the easiest way to round a decimal to 2 decimal places in Java? Thanks.
Sponsor
Sponsor
Sponsor
sponsor
andrew.




PostPosted: Sat Nov 20, 2010 10:04 pm   Post subject: RE:Rounding a Decimal to Two Decimal Places in Java

There may be a method for it, but you could just multiply by 100, round it, and then divide by 100.

e.g.
Java:
double result = 543.125;
result = Math.Round(result*100)/100;
SS1389




PostPosted: Thu Nov 25, 2010 10:17 pm   Post subject: Re: Rounding a Decimal to Two Decimal Places in Java

Yup. That was the method I was looking for. Thanks!
unoho




PostPosted: Thu Nov 25, 2010 11:04 pm   Post subject: RE:Rounding a Decimal to Two Decimal Places in Java

i remember our prof made us do some DecimalFormat function..
eragon5000




PostPosted: Fri Nov 26, 2010 11:10 pm   Post subject: Re: Rounding a Decimal to Two Decimal Places in Java

Yep Decimal Format is a lot easier to use:

code:
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(df.format(number));
SS1389




PostPosted: Thu Dec 09, 2010 5:37 pm   Post subject: Re: Rounding a Decimal to Two Decimal Places in Java

I think the *100, Math.round(), then /100 is much easier. Thank you everyone for your responses.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: