-1

I need to round up a double to the nearest highest int and I just stumbled across the ceil() method. I'm not quite sure what I'm doing wrong but it is not working as expected. I wrote this code to try to troubleshoot what I'm doing wrong but I can't figure out. I expected this to print '1.0' since .75 rounded up is 1.

    int d=3;
    int b= 4;
    double c=Math.ceil(d/b);
    System.out.println(c);
badsoup
  • 33
  • 4
  • 2
    3/4 = 0. You have to cast it to double or it will cast to int by itself. – Compass Jun 12 '18 at 17:05
  • Possible duplicate of [Rounding a double to turn it into an int (java)](https://stackoverflow.com/questions/2654839/rounding-a-double-to-turn-it-into-an-int-java) – Mehran Zamani Jun 12 '18 at 18:18

1 Answers1

1

you have to cast it first

 double c=Math.ceil((double)d/b);
Sundeep
  • 452
  • 2
  • 12
  • 2
    Or likewise, either `d` or `b` or both need to be of type `double` (rather than type `int`) – Bryan Jun 12 '18 at 17:08