40

Possible Duplicate:
round double to two decimal places in java

I want to round up the double value upto 2 decimal points.

for example: I have double d=2; and the result should be result =2.00

Community
  • 1
  • 1
Rakesh Sabbani
  • 1,655
  • 5
  • 17
  • 31

8 Answers8

67
Math.round(number*100.0)/100.0;
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    Nice and sweet. No need to user DecimalFormat. This solution shows a better performance for critical enviroments. – shimatai Jul 09 '16 at 22:22
  • Great solution, it avoids the library call to DecimalFomat! I think it's faster as well. – Al V Feb 01 '17 at 20:08
  • 3
    Try that with 33.605. When you multiply with 100.0 you get 3360.4999999999995, that will round to 33.60 and not to 33.61 as it should. – Papick G. Taboada Sep 06 '18 at 21:29
  • 1
    Best solution because it doesn't require any fancy things like external libraries, formatter objects etc. – hagrawal7777 May 06 '20 at 19:36
36
double RoundTo2Decimals(double val) {
            DecimalFormat df2 = new DecimalFormat("###.##");
        return Double.valueOf(df2.format(val));
}
Razan Paul
  • 13,618
  • 3
  • 69
  • 61
30

There's no difference in internal representation between 2 and 2.00. You can use Math.round to round a value to the nearest integer - to make that round to 2 decimal places you could multiply by 100, round, and then divide by 100, but you shouldn't expect the result to be exactly 2dps, due to the nature of binary floating point arithmetic.

If you're only interested in formatting a value to two decimal places, look at DecimalFormat - if you're interested in a number of decimal places while calculating you should really be using BigDecimal. That way you'll know that you really are dealing with decimal digits, rather than "the nearest available double value".

Another option you may want to consider if you're always dealing with two decimal places is to store the value as a long or BigInteger, knowing that it's exactly 100 times the "real" value - effectively storing cents instead of dollars, for example.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    @Rakesh: Are you just trying to *display* values? If so, that's fine... but be aware that, for example, `k` in your last example won't be *exactly* 4.144456 to start with. If exact decimal values matter to you, you shouldn't be using `double`. – Jon Skeet May 10 '11 at 06:15
  • 2
    @ Jon Skeet , but plain usage of BigDecimal sometimes doesn't correspondent with output from MsExcell (3,6,9) `int decimalPlace = 0; BigDecimal bd = new BigDecimal(someDoubleNumber); bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);` – mKorbel May 10 '11 at 06:57
  • 1
    @JonSkeet: what is the point of having double when we are not supposed to use it? – Jus12 Sep 16 '13 at 17:52
  • 1
    @Amitabh: Where did I say that you shouldn't be using `double` in any circumstances? I was talking about the situation where the exact decimal values are important - in particular with "man-made" values such as currency. `double` is appropriate for natural constructs such as height, weight etc. – Jon Skeet Sep 16 '13 at 18:25
  • 1
    It would be better with an actual static method that provides a solution – mjs Mar 31 '16 at 04:58
15
import java.text.DecimalFormat;

public class RoundTest {
    public static void main(String[] args) {
        double i = 2;    
        DecimalFormat twoDForm = new DecimalFormat("#.00");
        System.out.println(twoDForm.format(i));
        double j=3.1;
        System.out.println(twoDForm.format(j));
        double k=4.144456;
        System.out.println(twoDForm.format(k));
    }
}
Skylar Ittner
  • 802
  • 11
  • 26
Rakesh Sabbani
  • 1,655
  • 5
  • 17
  • 31
12

I guess that you need a formatted output.

System.out.printf("%.2f",d);
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
11

you can also use this code

public static double roundToDecimals(double d, int c)  
{   
   int temp = (int)(d * Math.pow(10 , c));  
   return ((double)temp)/Math.pow(10 , c);  
}

It gives you control of how many numbers after the point are needed.

d = number to round;   
c = number of decimal places  

think it will be helpful

Javed Akram
  • 15,024
  • 26
  • 81
  • 118
avirk
  • 3,050
  • 7
  • 38
  • 57
5

This would do it.

     public static void main(String[] args) {
        double d = 12.349678;
        int r = (int) Math.round(d*100);
        double f = r / 100.0;
       System.out.println(f);
     }

You can short this method, it's easy to understand that's why I have written like this.

Ankit
  • 2,753
  • 1
  • 19
  • 26
4
public static double addDoubles(double a, double b) {
        BigDecimal A = new BigDecimal(a + "");
        BigDecimal B = new BigDecimal(b + "");
        return A.add(B).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
Atish Narlawar
  • 670
  • 9
  • 15