Rounding up in java?

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
Is there a better way to round up numbers eg square roots than doing this? I can't find a method for rounding up in the Math class.

Code:
if (Math.sqrt(iterations) % 1 != 0) {
    System.out.println(Math.round(Math.sqrt(iterations) + 0.5));
} else {
    System.out.println(Math.round(Math.sqrt(iterations)));
}
 
Code:
public class lol {

	public static void main(String args[]){
		double lolz = 0.5111;
		double lolzy = Math.ceil(lolz);
		System.out.println(lolzy);
	}

}
 
Hells yeah.. I wrote my own method for it before spotting it :/
Code:
public int RoundUp(double aNumber)
{
  if ((int)aNumber > aNumber)
    return (int)aNumber;

  if ((int)aNumber < aNumber)
    return (int)aNumber + 1;

  return (int)aNumber;
}
 
Back
Top Bottom