0

Lets say I do some arithmetic computation (like division and then addition) on int values and then want to store the result in an int value as well.

How do I round values up that are greater than or equal to x.5 and then round down when they are less than x.5?

So 11.5 = 12 and 11.3 = 11

Kingamere
  • 9,496
  • 23
  • 71
  • 110

1 Answers1

1

The Java.Math library provides floor and ceiling functions.

   import Java.Math.*;

    System.out.println(Math.ceil(13.4)); // Prints 14
    System.out.println(Math.floor(13.4)); // Prints 13

There is also a rounding function provided in the same library

    System.out.println(Math.round(10.6)); // Prints 11
    System.out.println(Math.round(10.4)); // Prints 10
jrmullen
  • 346
  • 5
  • 21