Home >Java >javaTutorial >How to use round function in java
The round() function in Java rounds floating point numbers to the nearest integer, following the following rules: 1. Positive numbers are rounded to the nearest even number; 2. Negative numbers are rounded to the nearest even number; 3. When the decimal part is 0.5, round to the nearest even number.
Usage of round() function in Java
round() function overview
The round() function in Java is used to round floating point numbers to the nearest integer. This function accepts a floating point number as a parameter and returns a long value representing the rounded integer.
Syntax
<code class="java">public static long round(double a)</code>
Parameters
a
- The floating point number to be roundedReturn value
Rounding rules
The round() function follows the following rounding rules:
Example
The following example demonstrates how to use the round() function:
<code class="java">double num1 = 12.5; double num2 = -13.7; long roundedNum1 = Math.round(num1); // 12 long roundedNum2 = Math.round(num2); // -14</code>
In this example:
The above is the detailed content of How to use round function in java. For more information, please follow other related articles on the PHP Chinese website!