How to round numbers to four decimal place in Java?
<p>So let's say I have a simple array of float numbers</p>

<p>[0.315023, 1.0, 0.12345, 0121111]</p>

<p>And call it arr1.</p>

<p>I need to take this array and turn it into this:</p>

<p>[0.3150, 1.0000, 0.1234, 0.1211] *Please note that 1.0 is now 1.0000 and 0.3150 has a 0 at the end</p>

<p>I have tried using a for loop to iterate through the array and rounding the numbers as follows:</p>

<p>// i representing a number in the arr1; let's imagine that "newNum" replace the original numbers in arr1
float newNum = Math.round(i*10000.0000)/10000.0000;</p>

<p>What will result is following:</p>

<p>[0.315, 1.0, 0.1234, 0.1211]</p>

<p>As you can see, the first item is missing the 0 at the end, and the second item is missing a lot of zeros at the end.</p>

<p>I have tried to use DecimalFormat, but it doesn't work with the float array; also I am not trying to format decimals, I am rounding them. (although I have tried the round first then format second; which didn't work because of difference in datatype).</p>

<p>PLEASE for the love of God, help me. This is like the last thing I need to complete for my project. I am so close! Thank you so much in advance!</p>

<p>*PS: I need to make sure the array remains FLOAT; I need to use the data in the array later on.</p>