Java .round()

Bobliuuu's avatar
Published Oct 24, 2022
Contribute to Docs

The Math.round() method returns an int or long value that is closest to the number provided.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

Syntax

Math.round(n)

The .round() method takes one parameter n, the value that is to be rounded.

Example

The following example demonstrates using .round() to round the value 1.8 to the nearest integer:

public class Test {
public static void main(String args[]) {
int n = Math.round(1.8);
int m = Math.round(5.2);
System.out.println(n);
System.out.println(m);
}
}

This will produce the following output:

2
5

All contributors

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours