How to round float number in Android
<p>I am stuck in the scenario below:</p>

<p>If x is 1.5 or lower then the final result will be x = 1.
If x is large than 1.5 then x = 2.</p>

<p>The input number will be x/100.</p>

<p>For instance:
input = 0.015 => x = 1.5 => display x = 1.</p>

<p>The problem I got is that float number is inaccurate. For example:
input = 0.015 but actually it is something like 0.01500000000000002. In this case, x gonna be 1.500000000000002 which is large than 1.5 => display output is x = 2. </p>

<p>It happen so randomly which I don't know how to solve it. Like 0.5, 1.5 will give me the correct result. But 2.5, 3.5, 4.5, 5.5 will give me the wrong result. Then 6.5 will give me the correct result again.</p>

<p>The code I implemented is below:</p>

<pre><code>float x = 0.015;
NumberFormat nf = DecimalFormat.getPercentInstance();
nf.setMaximumFractionDigits(0);
output = nf.format(x);
</code></pre>

<p>So depends on x, the output might be right or wrong. It is just so random.</p>

<p>I alos tried to use Math.round, Math.floor, Math.ceils but none of them seems work since float number is so unpredictable.</p>

<p>Any suggestion for the solution?</p>

<p>Thanks in advance.</p>