Round float and double numbers in Java

In order to round float and double numbers in Java, we use the java.lang.Math.round() method. The method accepts either double or float values and returns an integer value. It returns the closest integer to number. This is computed by adding ½ to the number and then flooring it.

Declaration - The java.lang.Math.round() method is declared as follows −

public static int round(float a)
public static int round(double a)

where a is the value to be rounded.

Let us see a program where float and double numbers are rounded in Java.


 Live Demo

import java.lang.Math;
public class Example {
   public static void main(String[] args) {
      // declaring and initializing some double and values
      double x = 25.5;
      float y = 8.1f;
      // printing the rounded values
      System.out.println("The round-off of "+ x + " is "+ Math.round(x));
      System.out.println("The round-off of "+ y + " is "+ Math.round(y));


The round-off of 25.5 is 26
The round-off of 8.1 is 8

Updated on: 26-Jun-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started