Round float or double to integer number using Java Math class

This Java example shows how to round float or double number to an integer using the round method of the Java Math class.

How to round a number (float or double to integer)?

Use the round static method of the Java Math class to round a float or double number.

or

This method returns the closest integer number to the argument.

Output

Note: The round(float f)  method returns an int value while the round(double d)  method returns a long value.

Internally, 0.5 is added to the argument number and a floor value is taken. The result is then cast to an integer value as given below.

Output

Also, check out how to get the floor number and how to get the ceiling number examples.

This example is a part of the Java Math class tutorial with examples.

Please let me know your views in the comments section below.

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *