How can I convert integer into float in Java?
Solution 1
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;
Solution 2
// The integer I want to convert
int myInt = 100;
// Casting of integer to float
float newFloat = (float) myInt
Solution 3
You just need to transfer the first value to float, before it gets involved in further computations:
float z = x * 1.0 / y;
Solution 4
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.
Solution 5
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 !
Roman
Updated on December 22, 2020Comments
-
Roman over 3 years
I have two integers
x
andy
. I need to calculatex/y
and as outcome I would like to get float. For example as an outcome of3/2
I would like to have 1.5. I thought that easiest (or the only) way to do it is to convertx
andy
into float type. Unfortunately, I cannot find an easy way to do it. Could you please help me with that? -
Matt Ball over 13 yearsUse of a wrapper type (
Float
) is totally unnecessary for this. -
user229044 over 13 yearsPlease don't use signatures or taglines in your posts.
-
Matt Ball over 13 yearsYou should elaborate on why
double
is better thanfloat
. -
user unknown about 12 yearsYour 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.
-
MSquare over 10 yearsIs this more efficient, than: float z = (1.0 * x) / y; ? Is float conversion internally more efficient than multiplication? Tnx!
-
Matt Ball over 10 yearsI 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.
-
Oliver Dixon almost 10 yearsThe first and second one will cause errors on certain arm devices, make sure you cast both integers.
-
a.s.p. over 9 yearsjava.lang.Integer cannot be cast to java.lang.Float
-
Matt Ball over 9 years@user3002853 read about boxed types vs primitives. docs.oracle.com/javase/tutorial/java/data/autoboxing.html
-
Adam R. Turner over 5 yearsin Android Studio at least, the compiler will complain about incompatible types trying to do this.