How NOT to round up figures?

Asked

Viewed 112 times

1

I need an exact value but my code always rounds down:

float percentageUsed =  6050492416 / 8258998272;   
System.out.println(String.format("Porcentagem: %.2f", percentageUsed));

In case I had to return 0.73, but it always returns me 0.0.

How to get the exact result?

1 answer

2


At least one of the division numbers must be of the type float and is using integer, then the split is done as integer and then the entire result is saved in a variable of type float. Use the suffix f in the literal to say that the number is float. More simply:

System.out.printf("Porcentagem: %.2f", 6050492416f / 8258998272f);

In fact such a large number not even accept as a int, the code does not even compile. If you wanted an integer output you would have to use the suffix of long, the L to work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.