You just need to cast at least one of the operands to a float:

float z = (float) x / y;

or

float z = x / (float) y;

or (unnecessary)

float z = (float) x / (float) y;
Answer from Matt Ball on Stack Overflow
🌐
LabEx
labex.io › tutorials › java-java-integer-floatvalue-method-117710
Java Integer floatValue() Method | Programming Tutorial | LabEx
Learn how to use the Java Integer floatValue() method to convert an Integer object to its float equivalent. Detailed tutorial with examples.
Discussions

floating point - How to convert float to int with Java - Stack Overflow
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 ... More on stackoverflow.com
🌐 stackoverflow.com
java - Making a integer to a Float - Stack Overflow
I'm trying to convert an Integer to a Float since I need to use some math and need the decimals offered by floats. public static Float currAccu() { float accuMiss, accuHit, accuCrit; accuMiss = Float.parseFloat(publicInt.returnMissCount()); accuHit = Float.parseFloat(publicInt.returnHitCount()); accuCrit = Float.parseFloat(publicInt.returnMissCount()); return 0; The code is incomplete and I'm pretty new at Java... More on stackoverflow.com
🌐 stackoverflow.com
How do I cast my int to float?
To cast a value, you put the destination type in brackets before the value or variable you want to cast, for example (float)x would cast x to a float. However, I assume you're talking about L and S here, in which case just casting won't do you any good since you've declared them as int. They won't be able to hold any decimal places, so casting them to a float would just give you the same wrong number but as a float instead of an int. More on reddit.com
🌐 r/cs50
4
6
October 4, 2021
(Java) Need to convert from float to integer to floats for displaying a percentage. I have tried googling, yet can't seem to wrap my head around it
[1] initial value as a float, and then [2] convert it to an integer, [3] multiply by 1000 If you follow these steps exactly, you will lose information. Converting a float to an int drops the fraction component, so you have to do step 3 before step 2. More on reddit.com
🌐 r/learnprogramming
11
1
September 20, 2016
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › convert-int-to-float
Convert int to float - GeeksforGeeks
March 26, 2024 - # Using Explicit Type Conversion integer_value = 10 # Converting integer to float explicitly float_value = float(integer_value) print(float_value) # Output will be 10.0
🌐
Tutorialspoint
tutorialspoint.com › java › lang › number_floatvalue.htm
Java - Number floatValue() Method
The following example shows the usage of Java Number floatValue() method. An integer value is assigned to the Integer class reference variable; and the method is invoked on this object created to convert the integer value to a float value.
🌐
TutorialKart
tutorialkart.com › java › java-int-to-float
Convert Int to Float in Java
January 9, 2023 - Pass this integer variable as argument to intBitsToFloat() method. ... /** * Java Program - Convert Int to Float */ public class IntToFloat { public static void main(String[] args) { int i = 0x40000085; float f = Float.intBitsToFloat(i); ...
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.

🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › java › NUM50-J.+Convert+integers+to+floating+point+for+floating-point+operations
NUM50-J. Convert integers to floating point for floating-point operations - SEI CERT Oracle Coding Standard for Java - Confluence
See NUM13-J. Avoid loss of precision ... integer-to-floating-point conversion. Even with this loss of precision, however, the computed value of f is far more accurate than that produced in the noncompliant code example. This noncompliant code example attempts to compute the whole number greater than the ratio of two integers. The result of the computation is 1.0 rather than the intended 2.0. int a = 60070; int b = 57750; double value = Math.ceil(a/b); As a consequence of Java's numeric ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › integer-floatvalue-method-in-java
Integer floatValue() Method in Java - GeeksforGeeks
May 3, 2022 - --> java.lang Package --> Integer Class --> floatValue() Method · Syntax: public float floatValue() Return Type: A numeric value represented by this object after converting it to type float.
🌐
W3Docs
w3docs.com › java
How to convert float to int with Java
In this example, the f variable is a float with the value 123.45f. The (int) operator is used to convert the float to an int, and the result is assigned to the x variable.
🌐
Reddit
reddit.com › r/cs50 › how do i cast my int to float?
r/cs50 on Reddit: How do I cast my int to float?
October 4, 2021 -

int main (void)

{

string text = get_string("Text: ");

int Letters = count_letters(text);

int words = count_words(text);

int sen = count_sen(text);

int L = (100/words) * Letters;

int S = (100/words) * Letters;

int index = 0.0588 * L - 0.296 * S - 15.8;

int index1 = round(index);

printf("%i\n", Letters);

printf("%i\n", words);

printf("%i\n", sen);

printf("%i\n", index1);

}

🌐
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 - 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.
🌐
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.
🌐
Baeldung
baeldung.com › home › java › java numbers › make division of two integers result in a float
Make Division of Two Integers Result in a Float | Baeldung
January 8, 2024 - The test above passes if we give it a run. Therefore, casting any operand to float makes the division produce a float result. In this quick article, we’ve discussed why the division of integers always results in integers in Java.
🌐
Programming Idioms
programming-idioms.org › idiom › 79 › convert-integer-to-floating-point-number › 1463 › java
Convert integer to floating point number, in Java
Declare the floating point number y and initialize it with the value of the integer x . ... Do you know the best way to do this in your language ?
🌐
W3Schools
w3schools.com › java › ref_keyword_float.asp
Java float Keyword
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The float keyword is a data type that can store fractional numbers from 3.4e−038 to 3.4e+038.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Float.html
Float (Java Platform SE 8 )
2 weeks ago - If the argument is NaN, the result is 0x7fc00000. In all cases, the result is an integer that, when given to the intBitsToFloat(int) method, will produce a floating-point value the same as the argument to floatToIntBits (except all NaN values are collapsed to a single "canonical" NaN value).
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › lang › Float.html
Float (Java SE 21 & JDK 21)
January 20, 2026 - This method corresponds to the convertFormat operation defined in IEEE 754. ... Returns a hash code for this Float object. The result is the integer bit representation, exactly as produced by the method floatToIntBits(float), of the primitive float value represented by this Float object.
🌐
TutorialKart
tutorialkart.com › java › java-float-to-int
Java Program - Convert Float to Int
January 9, 2023 - Math.round() returns nearest integer value to the given floating point value. In this example, we shall convert floating point numbers with different precision and convert to int, to understand the rounding functionality.