Using Math.round() will round the float to the nearest integer.

Answer from tw39124 on Stack Overflow
Top answer
1 of 9
798

Using Math.round() will round the float to the nearest integer.

2 of 9
210

Actually, there are different ways to downcast float to int, depending on the result you want to achieve: (for int i, float f)

  • round (the closest integer to given float)

    i = Math.round(f);
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  2 ; f =  2.68 -> i =  3
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -3
    

    note: this is, by contract, equal to (int) Math.floor(f + 0.5f)

  • truncate (i.e. drop everything after the decimal dot)

    i = (int) f;
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  2 ; f =  2.68 -> i =  2
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -2
    
  • ceil/floor (an integer always bigger/smaller than a given value if it has any fractional part)

    i = (int) Math.ceil(f);
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  3 ; f =  2.68 -> i =  3
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -2
    
    i = (int) Math.floor(f);
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  2 ; f =  2.68 -> i =  2
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -3 ; f = -2.68 -> i = -3
    

For rounding positive values, you can also just use (int)(f + 0.5), which works exactly as Math.Round in those cases (as per doc).

You can also use Math.rint(f) to do the rounding to the nearest even integer; it's arguably useful if you expect to deal with a lot of floats with fractional part strictly equal to .5 (note the possible IEEE rounding issues), and want to keep the average of the set in place; you'll introduce another bias, where even numbers will be more common than odd, though.

See

http://mindprod.com/jgloss/round.html

http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html

for more information and some examples.

🌐
O'Reilly
oreilly.com › library › view › java-cookbook › 0596001703 › ch05s07.html
Rounding Floating-Point Numbers - Java Cookbook [Book]
June 21, 2001 - If you give it a float, you get an int. What if you don’t like the rounding rules used by round? If you wanted to round numbers greater than 0.54 instead of the normal 0.5, you could write your own version of round( ): // Round.java /** Round a number up if its fraction exceeds this threshold.
Author   Ian F. Darwin
Published   2001
Pages   888
🌐
Java67
java67.com › 2015 › 10 › how-to-convert-float-to-int-in-java-example.html
How to convert float to int in Java? Examples | Java67
* * @author WINDOWS * */ public class floatToInteger{ public static void main(String args[]) { // converting a float to int by casting System.out.println("converting float to int using downcasting"); int value = (int) 3.14f; // 3 int score = (int) 3.99f; // 3 System.out.printf("float : %f, int : %d %n", 3.14f, value); System.out.printf("float : %f, int : %d %n", 3.99f, score); // converting float to integer value by rounding System.out.println("converting float to int using rounding"); int a = Math.round(3.14f); // 3 int b = Math.round(3.99f); // 4 int c = Math.round(3.5f); // 4 System.out.pri
🌐
Baeldung
baeldung.com › home › java › java numbers › how to convert float to int in java
How to Convert float to int in Java | Baeldung
October 17, 2024 - If we need to convert a float to an int while rounding to the nearest whole number, we can use the Math.round() method. This method handles the decimal value appropriately and rounds up or down based on its value.
🌐
Study.com
study.com › courses › business courses › business 104: information systems and computer applications
How to Convert Float to Int in Java - Lesson | Study.com
March 12, 2018 - Java allows you to cast, or convert a variable of one type to another; in this case, cast a float to an int. This option removes all digits to the right of the decimal place. Another option is to use the Math.round method to round the value ...
🌐
TutorialKart
tutorialkart.com › java › java-float-to-int
Java Program - Convert Float to Int
January 9, 2023 - In this example, we shall convert floating point numbers with different precision and convert to int, to understand the rounding functionality. ... /** * Java Program - Convert Float to Integer */ public class FloatToInt { public static void main(String[] args) { float f; int n; f = 16.8564f; n = Math.round(f); System.out.println(n); f = 16.4235f; n = Math.round(f); System.out.println(n); } }
Find elsewhere
🌐
Quora
quora.com › What-is-the-easiest-way-to-get-an-integer-from-a-floating-point-number-in-Java-rounding
What is the easiest way to get an integer from a floating point number in Java (rounding)? - Quora
Answer (1 of 3): The easiest way to get an integer from a floating-point number in Java while rounding is to use the [code ]Math.round()[/code] method. This method takes a floating-point number as an argument and returns the nearest integer ...
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java float to int Conversion - Java Code Geeks
November 18, 2024 - The result is 543, with the fractional part .45 discarded. This method is simple and efficient but can lead to loss of precision. The Math.round() method provides a way to round a floating-point number to the nearest integer.
🌐
Scaler
scaler.com › home › topics › round off in java
Java Math.round() - Scaler Topics
April 19, 2024 - It takes a single argument of type double or float. It returns the rounded value as a long or an int, respectively. If the fractional part of the number is less than 0.5, it rounds down. If it's 0.5 or greater, it rounds up...
🌐
W3Docs
w3docs.com › java
How to convert float to int with Java
To convert a float to an int in Java, you can use the (int) type cast operator. The (int) operator will truncate the decimal part of the float value and convert it to an int.
🌐
Vertex Academy
vertex-academy.com › tutorials › en › rounding-numbers-java
Rounding of Numbers in Java • Vertex Academy
December 7, 2017 - Please note that this method returns a value of the int type (i.e. an integer). At first, we have 5.25 and the method gives us 5 instead of 5.0. 2. Math.floor () - this method rounds a number downward to the nearest integer.
🌐
TutorialsPoint
tutorialspoint.com › round-float-and-double-numbers-in-java
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 &frac
🌐
javaspring
javaspring.net › blog › how-to-convert-float-to-int-with-java
How to Convert Float to Int in Java Accurately: Best Methods for Rounding Instead of Truncation — javaspring.net
By default, Java truncates the decimal part of a float when casting to int, which can lead to unexpected results—especially in applications where precision matters (e.g., financial calculations, scientific measurements, or user-facing data).
🌐
Educative
educative.io › answers › how-to-use-the-java-mathround-method
How to use the Java Math.round() method
The Math.round() method in Java is used to round a number to its​ closest integer. This is done by adding ... 1/21/2 to the number, taking the floor of the result, and casting the result to an integer data type.
🌐
Delft Stack
delftstack.com › home › howto › java › java round up
How to Round Up a Number in Java | Delft Stack
March 4, 2025 - This article introduces how to round up any number in Java using the ceil() and floor() methods from the Math class. Discover practical examples and applications to enhance your programming skills in rounding numbers effectively.
🌐
Linux Hint
linuxhint.com › round-up-in-java
How to Round Up in Java
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use