Table of Contents

    Java round() Method: Usage and Examples

    Description

    This java tutorial shows how to use the round(float a) method of Math class under java.lang package. This method returns the absolute value of the double method argument.

    The method round returns the closest long or int, as given by the methods return type.

    Syntax

    This method has the following variants ?

    long round(double d)
    int round(float f)

    Parameters

    Here is the detail of parameters ?

    • d ? A double or float primitive data type.

    • f ? A float primitive data type.

    Return Value

    • This method returns the closest long or int, as indicated by the method's return type, to the argument.

    Example

    public class MathRoundMethod {
    
       public static void main(String args[]) {
          double d = 120.675;
          double e = 130.500;
          float f = 120;
          float g = 91f;
    
          System.out.println(Math.round(d));
          System.out.println(Math.round(e));
          System.out.println(Math.round(f));
          System.out.println(Math.round(g));
       }
    }

    Output

    121
    131
    120
    91
    Press any key to continue . . .

    Example

    public class RounFloatDoubleNumbersExample {
    
      public static void main(String[] args) {
    
    
         System.out.println(Math.round(10f));
    
    
         System.out.println(Math.round(20.5f));
    
    
         System.out.println(Math.round(20.5f));
    
    
         System.out.println(Math.round(-19.4f));
    
    
         System.out.println(Math.round(-23.5f));
    
      }
    }

    Output

    10
    21
    21
    -19
    -23
    Press any key to continue . . .