Easy Tutorial
❮ Dir Size Arrays Reverse ❯

Java round() Method

Java Number Class


The round() method returns the closest int or long value, rounded.

round means "round off", the algorithm is Math.floor(x+0.5), which means adding 0.5 to the original number and then rounding down, so Math.round(11.5) results in 12, and Math.round(-11.5) results in -11.

Syntax

The method has the following syntax formats:

long round(double d)

int round(float f)

Parameters

Return Value

Returns the closest int or long value, the method specifies the return data type.

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)); 
    }
}

Compiling the above program will output:

101
101
100
90

Java Number Class

❮ Dir Size Arrays Reverse ❯