How to pass a float without loss of value to an integer?

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 about using some test to see if the numbers after the comma are 0's or not. Does anyone know how?

    
asked by anonymous 17.07.2017 / 22:50

2 answers

3

Only with% w /% can test whether the value that the variable has is integer or not, verifying that the value matches the truncated value of the variable, using if :

float f = 1.0f;

if (f == Math.floor(f)){ //que irá dar verdadeiro

Test example

    
17.07.2017 / 22:59
0

Well, when you convert a float, double, or other type that has decimals to integer, you will always have the decimal part lost.

It's also worth noting that float and double are not accurate, if you want precision see BigDecimal.

If you are aware of all this, you simply have to compare the values with the Integer.MAX_VALUE and Integer.MIN_VALUE constants:

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 program exit:

** 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)
    
17.07.2017 / 23:45