2
I have a float variable that can contain any number in it, I need to test if it fits an integer without losing values. I thought to use some test to see if the numbers after the comma are 0’s or not. Does anyone know how?
2
I have a float variable that can contain any number in it, I need to test if it fits an integer without losing values. I thought to use some test to see if the numbers after the comma are 0’s or not. Does anyone know how?
3
Only with a if
can test whether the value the variable has is integer or not, by checking whether the value matches the truncated value of the variable, using the Math.floor
:
float f = 1.0f;
if (f == Math.floor(f)){ //que irá dar verdadeiro
0
Well, when you are converting a float, double or other type that has decimal to integer, you will always have the loss of the decimal part.
Also worth noting that float and double are not accurate, if you want accuracy see the Bigdecimal.
If you are aware of all this, just compare the values with the Integer constants.MAX_VALUE and Integer.MIN_VALUE:
float f = 123.456f;
if(f <= Integer.MAX_VALUE && f >= Integer.MIN_VALUE) {
// parte inteira de f está dentro da faixa do int
}
Below is a test that demonstrates some problems involving floating point and its output:
public class TestesSO {
public static void main(String[] args) {
float floats[] = { 1.0f, 2.1f, 3.3333f, 4.4f, 5.999999999f, 6.0f, (float) Integer.MAX_VALUE, (float) Integer.MAX_VALUE + 0.1f,
(float) Integer.MIN_VALUE, (float) Integer.MIN_VALUE - 0.1f };
double doubles[] = { 1.0, 2.1, 3.3333, 4.4, 5.999999999999, 6.0, (double) Integer.MAX_VALUE, (double) Integer.MAX_VALUE + 0.1,
(double) Integer.MIN_VALUE, (double) Integer.MIN_VALUE - 0.1 };
System.out.println("** floats **");
for (int i = 0; i < floats.length; i++) {
float f = floats[i];
int n = (int) f;
System.out.printf("%15d == %15.15f ? %b (%s em um int)%n", n, f, (float) n == f,
(f <= Integer.MAX_VALUE && f >= Integer.MIN_VALUE) ? "cabe" : "não cabe");
}
System.out.println("** doubles **");
for (int i = 0; i < doubles.length; i++) {
double d = doubles[i];
int n = (int) d;
System.out.printf("%15d == %15.15f ? %b (%s em um int)%n", n, d, (double) n == d,
(d <= Integer.MAX_VALUE && d >= Integer.MIN_VALUE) ? "cabe" : "não cabe");
}
}
}
Follow the output of the program:
** floats **
1 == 1,000000000000000 ? true (cabe em um int)
2 == 2,099999904632568 ? false (cabe em um int)
3 == 3,333300113677979 ? false (cabe em um int)
4 == 4,400000095367432 ? false (cabe em um int)
6 == 6,000000000000000 ? true (cabe em um int)
6 == 6,000000000000000 ? true (cabe em um int)
2147483647 == 2147483648,000000000000000 ? true (cabe em um int)
2147483647 == 2147483648,000000000000000 ? true (cabe em um int)
-2147483648 == -2147483648,000000000000000 ? true (cabe em um int)
-2147483648 == -2147483648,000000000000000 ? true (cabe em um int)
** doubles **
1 == 1,000000000000000 ? true (cabe em um int)
2 == 2,100000000000000 ? false (cabe em um int)
3 == 3,333300000000000 ? false (cabe em um int)
4 == 4,400000000000000 ? false (cabe em um int)
5 == 5,999999999999000 ? false (cabe em um int)
6 == 6,000000000000000 ? true (cabe em um int)
2147483647 == 2147483647,000000000000000 ? true (cabe em um int)
2147483647 == 2147483647,100000000000000 ? false (não cabe em um int)
-2147483648 == -2147483648,000000000000000 ? true (cabe em um int)
-2147483648 == -2147483648,100000000000000 ? false (não cabe em um int)
Browser other questions tagged java type-conversion float
You are not signed in. Login or sign up in order to post.