How to Round Float and Double Numbers in Java


In this example we will show how to round given float or double number using round method of Math class in Java.

Source Code

package com.beginner.examples;

public class RoundExample {

	public static void main(String[] args) {

		double x1 = 6;
		System.out.println(Math.round(x1));
		
		float x2 = 6.7f;
		System.out.println(Math.round( x2));
		
		double x3 = -6.1;
		System.out.println(Math.round(x3));
		
		float x4 = -5f;
		System.out.println(Math.round(x4));
    }
}

Output:

6
7
-6
-5
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments