Java: trying to get the percentage of integers, how to sort it out?
•
Java
I have two integer values, X and the sum I tried to find the percentage of total X That's what I'm doing now:
Percentage = (int) ((x * 100) / total);
Percentage must be an integer When I do this, it always rounds down the decimal point Is there a simple way to calculate the percentage as an integer, and if the decimal point is. 5 or higher, it will be rounded up?
Solution
Using math Round (x * 100.0 / total) But note that this returns a long, so you need to simply convert int
I forced 100.0 to use floating point before rounding
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码