I have two integers x
and y
. I need to calculate x/y
and as outcome I would like to get float. For example as an outcome of 3/2
I would like to have 1.5. I thought that easiest (or the only) way to do it is to convert x
and y
into float type. Unfortunately, I cannot find an easy way to do it. Could you please help me with that?
-
8float result = ((float)x) / y? – mibollma Dec 07 '10 at 14:43
-
check this out: http://xahlee.org/java-a-day/casting.html – fasseg Dec 07 '10 at 14:45
-
2possible duplicate of [Java: dividing 2 ints makes an int?](http://stackoverflow.com/questions/787700/java-dividing-2-ints-makes-an-int) – dogbane Dec 07 '10 at 14:54
-
I Would suggest you use BigDecimal because floating point (Double, Float) representations and calculations are inexact, leading to erroneous results. – Vinay Lodha Dec 07 '10 at 15:05
-
1@VinAy: I would **not** recommend that in this case. `BigDecimal` is not a universally correct replacement for `float`. – Matt Ball Dec 07 '10 at 15:09
-
@Matt: Yeah I agree.. but accuracy in Math operations is very important. Better not to write buggy program :) – Vinay Lodha Dec 07 '10 at 15:18
-
1@VinAy: It depends entirely what the purpose is. If he is doing finance, you are correct. If he is computing asteroid trajectories, you aren't. – user207421 Dec 07 '10 at 22:45
-
I usually just multiply by 1.0, is that a bad idea? Like float z = x*1.0/y; – mohdajami Feb 23 '12 at 07:47
-
Great question, well researched as well. – vikramvi Aug 08 '23 at 01:46
6 Answers
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;

- 354,903
- 100
- 647
- 710
-
Is this more efficient, than: float z = (1.0 * x) / y; ? Is float conversion internally more efficient than multiplication? Tnx! – MSquare Sep 13 '13 at 09:34
-
1I don't know, but I think it's irrelevant 99% or more of the time. It's not even remotely going to be a bottleneck. If you're truly that concerned, [benchmark it yourself.](http://stackoverflow.com/q/504103/139010) – Matt Ball Sep 13 '13 at 14:15
-
1The first and second one will cause errors on certain arm devices, make sure you cast both integers. – Oliver Dixon Jul 26 '14 at 01:00
-
-
2@user3002853 read about boxed types vs primitives. http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html – Matt Ball Oct 06 '14 at 12:28
// The integer I want to convert
int myInt = 100;
// Casting of integer to float
float newFloat = (float) myInt

- 2,995
- 2
- 25
- 35
-
2in Android Studio at least, the compiler will complain about incompatible types trying to do this. – Adam R. Turner Jan 07 '19 at 00:10
You just need to transfer the first value to float, before it gets involved in further computations:
float z = x * 1.0 / y;

- 35,537
- 11
- 75
- 121
You shouldn't use float unless you have to. In 99% of cases, double is a better choice.
int x = 1111111111;
int y = 10000;
float f = (float) x / y;
double d = (double) x / y;
System.out.println("f= "+f);
System.out.println("d= "+d);
prints
f= 111111.12
d= 111111.1111
Following @Matt's comment.
float has very little precision (6-7 digits) and shows significant rounding error fairly easily. double has another 9 digits of accuracy. The cost of using double instead of float is notional in 99% of cases however the cost of a subtle bug due to rounding error is much higher. For this reason, many developers recommend not using floating point at all and strongly recommend BigDecimal.
However I find that double can be used in most cases provided sensible rounding is used.
In this case, int x has 32-bit precision whereas float has a 24-bit precision, even dividing by 1 could have a rounding error. double on the other hand has 53-bit of precision which is more than enough to get a reasonably accurate result.

- 525,659
- 79
- 751
- 1,130
Here is how you can do it :
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 3;
int y = 2;
Float fX = new Float(x);
float res = fX.floatValue()/y;
System.out.println("res = "+res);
}
See you !

- 232,980
- 40
- 330
- 338

- 10,265
- 1
- 33
- 41
-
7
-
1Please [don't use signatures or taglines](http://stackoverflow.com/faq#signatures) in your posts. – user229044 Dec 07 '10 at 15:29
Sameer:
float l = new Float(x/y)
will not work, as it will compute integer division of x and y first, then construct a float from it.
float result = (float) x / (float) y;
Is semantically the best candidate.

- 61
- 3
-
Your answer should have been a comment. Sameer will receive no notification of your post. Semantically, converting both ints to float before computing the result is needless - therefore it isn't better, than tranforming just one. It gives a wrong impression, and is therefore inferior, imho. – user unknown Apr 28 '12 at 00:40