Java Math Round() - Programiz

Tutorials Courses Python JavaScript TypeScript SQL HTML CSS C C++ Java R Ruby RUST Golang Kotlin Swift C# DSA

Become a certified Java programmer.

ENROLL

Popular Tutorials

Java "Hello World" Program Java for Loop Arrays in Java Interfaces in Java Java ArrayList Start Learning Java

Popular Examples

Check prime number Print the Fibonacci series Print Pyramids and Patterns Multiply two matrices Find the standard deviation Explore Java Examples

Reference Materials

String ArrayList HashMap Math View all Programiz Pro Logo

Created with over a decade of experience.

  • Learn
  • Practice
  • Compete
Learn Python Learn HTML Learn JavaScript Learn SQL Learn DSA Learn C Learn C++ Learn Java View all Courses on Programiz Pro Logo Python Basics Python Intermediate C++ Basics C++ Intermediate C++ OOP C Programming Java Basics Java Intermediate Java OOP View all Courses on Programiz Pro Logo Python Challenges JavaScript Challenges Java Challenges C++ Challenges C Challenges View all Challenges on Programiz Pro Logo Learn Practice Compete

Certification Courses

Created with over a decade of experience and thousands of feedback.

Learn Python Learn HTML Learn JavaScript Learn SQL Learn DSA View all Courses on Programiz Pro Logo Learn C Learn C++ Learn Java Python JavaScript TypeScript SQL HTML CSS C C++ Java More languages

Become a certified Java programmer.

Try Programiz PRO!

Popular Tutorials

Java "Hello World" Program Java for Loop Arrays in Java Interfaces in Java Java ArrayList Start Learning Java All Java Tutorials

Reference Materials

String ArrayList HashMap Math View all Python JavaScript C C++ Java R Kotlin

Become a certified Java programmer.

Try Programiz PRO!

Popular Examples

Check prime number Print the Fibonacci series Print Pyramids and Patterns Multiply two matrices Find the standard deviation All Java Examples

Java Math Methods

  • Java Math.abs()
  • Java Math.acos()
  • Java Math.addExact()
  • Java Math.asin()
  • Java Math.atan()
  • Java Math.cos()
  • Java Math.sin()
  • Java Math.tan()
  • Java Math.sinh()
  • Java Math.cosh()
  • Java Math.tanh()
  • Java Math.sqrt()
  • Java Math.cbrt()
  • Java Math.pow()
  • Java Math.subtractExact()
  • Java Math.multiplyExact()
  • Java Math.incrementExact()
  • Java Math.decrementExact()
  • Java Math.negateExact()
  • Java Math.toIntExact()
  • Java Math.min()
  • Java Math.max()
  • Java Math.ceil()
  • Java Math.floor()
  • Java Math.round()
  • Java Math.toRadians()
  • Java Math.toDegrees()
  • Java Math.atan2()
  • Java Math.copySign()
  • Java Math.exp()
  • Java Math.expm1()
  • Java Math.getExponent()
  • Java Math.hypot()
  • Java Math.IEEEremainder()
  • Java Math.log()
  • Java Math.log10()
  • Java Math.log1p()
  • Java Math.nextAfter()
  • Java Math.nextUp()
  • Java Math.nextDown()
  • Java Math.rint()
  • Java Math.random()

Java Tutorials

  • Java Math floor()
  • Java Math ceil()
  • Java Math rint()
  • Java Math.random()
  • Java Math abs()
  • Java Math max()
Java Math round()

The round() method rounds the specified value to the closest int or long value and returns it. That is, 3.87 is rounded to 4 and 3.24 is rounded to 3.

Example

class Main { public static void main(String[] args) { double a = 3.87; System.out.println(Math.round(a)); } } // Output: 4

Syntax of Math.round()

The syntax of the round() method is:

Math.round(value)

Here, round() is a static method. Hence, we are accessing the method using the class name, Math.

round() Parameters

The round() method takes a single parameter.

  • value - number which is to be rounded

Note: The data type of the value should be either float or double.

round() Return Value

  • returns the int value if the argument is float
  • returns the long value if the argument is double

The round() method:

  • rounds upward if the value after the decimal is greater than or equal to 5 1.5 => 2 1.7 => 2
  • rounds downward if the value after the decimal is smaller than 5 1.3 => 1

Example 1: Java Math.round() with double

class Main { public static void main(String[] args) { // Math.round() method // value greater than 5 after decimal double a = 1.878; System.out.println(Math.round(a)); // 2 // value equals to 5 after decimal double b = 1.5; System.out.println(Math.round(b)); // 2 // value less than 5 after decimal double c = 1.34; System.out.println(Math.round(c)); // 1 } }

Example 2: Java Math.round() with float

class Main { public static void main(String[] args) { // Math.round() method // value greater than 5 after decimal float a = 3.78f; System.out.println(Math.round(a)); // 4 // value equals to 5 after decimal float b = 3.5f; System.out.println(Math.round(b)); // 4 // value less than 5 after decimal float c = 3.44f; System.out.println(Math.round(c)); // 3 } }

Also Read:

  • Java Math.floor()
  • Java Math.ceil()
Previous Tutorial: Java Math.floor() Next Tutorial: Java Math.toRadians Share on: Did you find this article helpful?

Sorry about that.

How can we improve it? Feedback * Leave this field blank

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community

Related Library Functions

Java Library

Java Math floor()

Java Library

Java Math ceil()

Java Library

Java Math rint()

Java Library

Java Math.random()

Tag » How To Round Up Java