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.

🌐
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 - In scenarios where we need greater control over the rounding behavior, Java’s Math class provides two useful methods: The Math.floor() method consistently rounds a given float value down to the nearest whole number.
🌐
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 lets you cast variables, i.e. convert a variable of one type to another. You basically tell Java that you want the float variable to act like it's an int. When Java casts it down to the integer, it chops off everything to the right of the decimal point. Let's look at how this is done with code.
🌐
LabEx
labex.io › tutorials › java-java-float-intvalue-method-117662
Java Float to Int Conversion | Programming Tutorial | LabEx
import java.util.Scanner; public class FloatToInt { public static void main(String[] args) { Scanner sc = new Scanner(System.in); try { // Getting the user input as float value System.out.print("Enter a floating point value: "); float f = sc.nextFloat(); // Creating a Float object from the user input Float f1 = f; // Converting the Float object to int int i = f1.intValue(); // Displaying the int value System.out.println("Float value: " + f1); System.out.println("Int value: " + i); } catch (Exception e) { System.out.println("Invalid input.
🌐
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
... Even though both float and int are 32-bit wide data types, the float has a higher range than the integer primitive value. Since a float is bigger than int, you can convert a float to an int by simply down-casting it e.g.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java float to int Conversion - Java Code Geeks
November 18, 2024 - Both methods offer more control over rounding behaviour compared to simple type casting or Math.round(). However, like all floating-point to integer conversions, they may still encounter issues with range overflow if the float value is too large for an int.
🌐
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.
🌐
Processing Forum
forum.processing.org › one › topic › converting-floats-to-ints.html
Converting floats to ints - Processing Forum
I tried int x = (int)(a + b); Now this worked because it added the 2 floats then cast the answer to an int. So it looks like the answer to your question is - yes. I'm really used to using the Processing built in one, the Java method is fairly alien to me still.
Find elsewhere
🌐
Oracle
docs.oracle.com › en › java › javase › 24 › docs › api › java.base › java › lang › Float.html
Float (Java SE 24 & JDK 24)
July 15, 2025 - Returns the float value corresponding to a given bit representation. ... Returns the value of this Float as an int after a narrowing primitive conversion.
🌐
Simplesolution
simplesolution.dev › java-convert-float-to-int
Convert float to int in Java
public class ConvertFloatToIntExample2 { public static void main(String[] args) { Float value1 = 12.34F; int value2 = value1.intValue(); System.out.println("float value: " + value1); System.out.println("int value: " + value2); } } The output is:
🌐
Netjstech
netjstech.com › 2016 › 09 › converting-float-to-int-java-program.html
Convert float to int in Java | Tech Tutorials
Java code examples and interview questions. Spring code examples. ... If you have a float number and you have a requirement in your application at some place to show number without decimal points then one of the option is to convert that float type to int and display the value, that’s what ...
🌐
TutorialKart
tutorialkart.com › java › java-float-to-int
Java Program - Convert Float to Int
January 9, 2023 - We shall discuss two ways: Firstly, down or narrowing casting, where manual typecasting of higher datatype to lower datatype is done; Secondly, Math.round() function, which returns a rounded value of given float value.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › convert-int-to-float
Convert int to float - GeeksforGeeks
March 26, 2024 - Converting an integer to a floating-point number involves changing the data type of the variable from int to float, allowing decimal values. This conversion is useful when performing arithmetic operations that result in fractional values or when working with numerical data that requires precision beyond whole numbers.
🌐
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 ...
🌐
Reddit
reddit.com › r/processing › help for beginner: cannot convert from float to int
r/processing on Reddit: help for beginner: cannot convert from float to int
October 6, 2019 -

hi all.

I've just started learning processing. I'm trying to create a 50 value float array where each value is 0.5 larger than the last, starting at zero using a for loop. I'm getting an error "cannot convert from float to int."

I'm not sure what's wrong. Any help?

float[] four = new float[50];

for(float i=0; i<four.length;i++){

four[i]=i*0.5;

}

TIA.

🌐
Coderanch
coderanch.com › t › 388469 › java › Float-Int
Float to Int(VERY URGENT) (Beginning Java forum at Coderanch)
The code below shows how float does not store large non decimal values accurately : class c{ { public static void main(String ja[]) int x = 312345678; float y = 312345678f; System.out.println(x ); System.out.println(y ); } } The output is : 312345678 // int value 3.12345664E8 // float value
🌐
Tek-Tips
tek-tips.com › home › forums › software › programmers › languages › java
WHY would you cast a float to an int?... | Tek-Tips
October 18, 2000 - Converting float to int, is just a way to round a float number, that's all I think. Do you see what I mean? ... yes mr globos is write . here in java casting is in simple english converting flaot type variables to Integer or Float objects .' This is called wrapping .