java tutorial - Java Math.round() Method - tutorial java - java programming - learn java - java basics - java for beginners



Java numbers round

Learn Java - Java tutorial - Java numbers round - Java examples - Java programs

Description

Method Math.round () - returns an integer, long or int, closest to a real number, double or float, of an argument. In other words, it rounds the fraction to an integer.

Syntax

The method has the following rounding options to an integer:

long round(double d)
int round(float f)
click below button to copy the code. By - java tutorial - team

Options

Detailed information about the parameters:

  • d - double or float is a primitive data type.
  • f is the float data type.

Return value

  • In Java, Math.round () returns an integer (long or int), closest to a real number, double or float, of an argument.

Sample Code

public class Test { 

   public static void main(String args[]) {
      double d1 = 1.49;
      double d2 = 1.50;
      double d3 = 1.75;
      double d4 = -1.49;
      double d5 = -1.50;
      double d6 = -1.75;
      
      float f1 = 10;
      float f2 = 9f;
      float f3 = -10;
      float f4 = -9f;

      System.out.println("d1 = " + Math.round(d1));
      System.out.println("d2 = " + Math.round(d2)); 
      System.out.println("d3 = " + Math.round(d3)); 
      System.out.println("d4 = " + Math.round(d4)); 
      System.out.println("d5 = " + Math.round(d5)); 
      System.out.println("d6 = " + Math.round(d6)); 
      System.out.println("-----------"); 
      System.out.println("f1 = " + Math.round(f1)); 
      System.out.println("f2 = " + Math.round(f2));
      System.out.println("f3 = " + Math.round(f3));
      System.out.println("f4 = " + Math.round(f4)); 
   }
}
click below button to copy the code. By - java tutorial - team

Output

d1 = 1
d2 = 2
d3 = 2
d4 = -1
d5 = -1
d6 = -2
-----------
f1 = 10
f2 = 9
f3 = -10
f4 = -9

Related Searches to Java Math.round() Method