Java Math Round() - Programiz
Maybe your like
Become a certified Java programmer.
ENROLLPopular Tutorials
Java "Hello World" Program Java for Loop Arrays in Java Interfaces in Java Java ArrayList Start Learning JavaPopular Examples
Check prime number Print the Fibonacci series Print Pyramids and Patterns Multiply two matrices Find the standard deviation Explore Java ExamplesReference Materials
String ArrayList HashMap Math View allCreated with over a decade of experience.
- 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 onBecome 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 TutorialsReference Materials
String ArrayList HashMap Math View all Python JavaScript C C++ Java R KotlinBecome 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 ExamplesJava 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()
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: 4Syntax 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()
Sorry about that.
How can we improve it? Feedback * Leave this field blankYour 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
-
Rounding Of Numbers In Java - Vertex Academy
-
Java Round Up Any Number - Math - Stack Overflow
-
How To Round Up In Java - Linux Hint
-
Round Up A Number In Java | Delft Stack
-
How To Round Up In Java Code Example
-
How To Use The Java Math.round() Method
-
How To Round A Number To N Decimal Places In Java - Baeldung
-
How To Round Up To 2 Decimal Places In Java? - Intellipaat Community
-
4 Examples To Round Floating-Point Numbers In Java Up To ... - Java67
-
How Do I Round A Decimal To Nearest Integer In Java? - Quora
-
Java Math Round() Method With Example - GeeksforGeeks
-
Java Program To Round A Number To N Decimal Places
-
Java Math.round() Method With Examples - Javatpoint
-
Round Up Int [Solved] (Beginning Java Forum At Coderanch)