Author |
Message |
SS1389
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
andrew.
|
Posted: 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; |
|
|
|
|
|
![](images/spacer.gif) |
SS1389
|
Posted: 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! |
|
|
|
|
![](images/spacer.gif) |
unoho
![](http://compsci.ca/v3/uploads/user_avatars/9594019524c18337ec70d8.jpg)
|
Posted: 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.. |
|
|
|
|
![](images/spacer.gif) |
eragon5000
|
Posted: 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)); |
|
|
|
|
|
![](images/spacer.gif) |
SS1389
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
|