Round a value to integer

ReturnMethodSummary
static longround(double a)Returns the closest long to a.
static intround(float a)Returns the closest int to a.

public class Main {
  public static void main(String[] args) {

    System.out.println(Math.round(1.2));
    System.out.println(Math.round(1.9));
  }
}

The output:


1
2

Round Java float and double numbers using Math.round


public class Main {
  public static void main(String[] args) {
    System.out.println(Math.round(1.3f));
    System.out.println(Math.round(2.5f));
    System.out.println(Math.round(2.5f));
    System.out.println(Math.round(-9.4f));
    System.out.println(Math.round(-3.5f));
  }
}

The output:


1
3
3
-9
-3
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.