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
๐ŸŒ
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.
Discussions

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
floating point - How to convert float to int with Java - Stack Overflow
I used the following line to convert float to int, but it's not as accurate as I'd like: float a=8.61f; int b; b=(int)a; The result is : 8 (It should be 9) When a = -7.65f, the result is : -7... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Converting floats to ints - Processing Forum
The second example is part of the Java language and is called type-casting. It is can only be used to convert numeric and char types to integer. So it is fine if the data type is one of the following char short long float double but will not work with strings. More on forum.processing.org
๐ŸŒ forum.processing.org
October 20, 2012
(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
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ java-int-to-float
Convert Int to Float in Java
January 9, 2023 - In the following example, we have a variable i which is of int type. We shall assign i to float variable f during its declaration. ... /** * Java Program - Convert Int to Float */ public class IntToFloat { public static void main(String[] args) { int i = 125; float f = i; System.out.println(f); } }
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ convert int into float java
How to Convert Int to Float in Java | Delft Stack
February 2, 2024 - Subsequently, the program uses System.out.println() to display the value of f. In this case, it will print 56.0 since float can accurately represent integer values without any loss of precision. ... The above code serves as a clear example of how Java handles implicit type casting, making it convenient for developers to work with different data types in their programs.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ Float.html
Float (Java Platform SE 8 )
April 21, 2026 - No IEEE 754 floating-point operation provided by Java can distinguish between two NaN values of the same type with different bit patterns. Distinct values of NaN are only distinguishable by use of the Float.floatToRawIntBits method. In all other cases, let s, e, and m be three values that can be computed from the argument: int s = ((bits >> 31) == 0) ?
๐ŸŒ
Simplesolution
simplesolution.dev โ€บ java-convert-int-to-float
Convert int to float in Java - Simple Solution
With an Integer object in Java we can use the Integer.floatValue() method to return float value from a given Integer object as Java example code below.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-java-integer-floatvalue-method-117710
Java Integer floatValue() Method | Programming Tutorial | LabEx
In this lab, you will learn about the Java floatValue() method of the Integer class, which is used for converting an Integer object into its float equivalent. You will also learn about the syntax, parameters, and return value of this method.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ integer-floatvalue-method-in-java
Integer floatValue() Method in Java - GeeksforGeeks
May 3, 2022 - ... // Java Program to Demonstrate ... Integer a = new Integer(28); // Converting Integer number to float value // using floatValue() method float b = a.floatValue(); // Printing the float value System.out.println(b); ...
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 10 โ€บ java-integer-floatvalue-method
Java Integer floatValue() Method
public class JavaExample { public static void main(String[] args) { Integer i = new Integer("102"); Integer i2 = new Integer(15); float d = i.floatValue(); float d2 = i2.floatValue(); System.out.println("Value of Integer i as float: "+d); System.out.println("Value of Integer i2 as float: "+d2); } }
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ lang โ€บ number_floatvalue.htm
Java - Number floatValue() Method
package com.tutorialspoint; public class NumberFloatVal { public static void main(String[] args) { // get a number as integer Integer x = 123456; // print their value as float System.out.println("x as integer: " + x + ", x as float: " + x.floatValue()); } } Let us compile and run the above program, this will produce the following result โˆ’ ... In this example, we first assign a long value to a Long class reference 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 โ€บ business courses โ€บ business 104: information systems and computer applications
How to Convert Float to Int in Java - Lesson | Study.com
March 12, 2018 - Java has classes for all types like Double = double, Float = float, and Integer = integer. These classes are called wrapper classes in Java because they wrap the primitive data type within their 'arms'.
๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ article โ€บ the-complete-java-type-casting-tutorial
Type Casting in Java
August 7, 2023 - The int and float each take 4 bytes of memory. The int holds a whole number, and the float holds a floating-point number. Assignment of int to float is done automatically by JVM.
Top answer
1 of 9
799

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.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-number-floatvalue-method
Java Number.floatValue() Method - GeeksforGeeks
May 15, 2025 - Integer to float: 1.78542349E9 Double to float: 9.785488E7 ยท Example 2: In this example, we are going to take smaller values with Integer and Double. Java ยท
๐ŸŒ
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.
๐ŸŒ
Processing Forum
forum.processing.org โ€บ topic โ€บ converting-floats-to-ints
Converting floats to ints - Processing Forum
October 20, 2012 - 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.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 411858 โ€บ java โ€บ convert-float-int
how to convert float to int? (Beginning Java forum at Coderanch)
Originally posted by Anto telvin Mathew: hi all how to convert float to int manually using paper You already asked how to manually convert between floating point numbers which requires you to understand the IEEE format. You already asked how to convert amoung the whole numbers, which requires you to understand twos complements.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2015 โ€บ 09 โ€บ how-to-convert-float-to-long-in-java.html
How to convert float to long or int data type in Java? Example
I asked him whether he needs routing or not, which he wasn't sure but it turns out that he didn't need that. I explained to him how to do that and that's what you will find in this article as well. In short, there are 3 ways to convert a float value into long or int in Java, but we will only focus on the long data type part.