Java round() Method

Java Number类Java Number Class


The round() method returns a closest int or long value, with rounding.

roundmeans "rounding", the algorithm isMath.floor(x+0.5), that is, add 0.5 to the original number and then round down, soMath.round(11.5)the result is 12, and the result of Math.round(-11.5) is -11.

Syntax

This method has the following syntax formats:

long round(double d)

int round(float f)

Parameters

  • d -- double or float primitive data type

  • f -- float primitive data type

Return Value

Returns a closest int or long value; the method specifies the returned data type.

Example

Example

public class Test{ public static void main(String args[]){ double d = 100.675; double e = 100.500; float f = 100; float g = 90f; System.out.println(Math.round(d)); System.out.println(Math.round(e)); System.out.println(Math.round(f)); System.out.println(Math.round(g)); } }

Compile the above program, and the output result is:

101
101
100
90

Java Number类Java Number Class

Other Extensions