📜  math.round java (1)

📅  最后修改于: 2023-12-03 15:02:53.019000             🧑  作者: Mango

Math.round() in Java

The Math.round() method in Java is a built-in function that rounds a specified expression or value to the nearest integer.

Syntax
public static long round(double value)

where value specifies the value to be rounded.

Return Value

The Math.round() method returns the long value representing the rounded integer.

Example
double num = 3.14159;
long roundedValue = Math.round(num);
System.out.println("Rounded value of " + num + " is " + roundedValue);

Output:

Rounded value of 3.14159 is 3
Additional Information
  • If the argument is NaN, the result is 0.
  • The value to be rounded can also be a float data type, but the return type will still be long.
  • The Math.round() method uses the RoundingMode.HALF_UP mode as its default rounding mode.

So, this was a brief introduction to the Math.round() method in Java, which is very useful for mathematical calculations in Java programs.