In my understanding, in half rounding, if the number that you round is evenly spaced between its neighbors (for example, 4.5, if you round one after the decimal), you round the previous number to the nearest even number.
For example, if you round to 0 digits after the decimal, 1.5, 3.5, 5.5, 7.5, and 9.5 will be rounded to 2, 4, 6, 8, and 10, respectively. All this works well and perfectly in netbeans, but I run into problems when I round to 1 place after the decimal point.
package foo; import java.text.NumberFormat; public class Foo { public static void main(String[] args) { NumberFormat formatter = NumberFormat.getNumberInstance(); formatter.setMaximumFractionDigits(1); System.out.print(formatter.format(4.45)); } }
run:
4.5 BUILD SUCCESSFUL (total time: 0 seconds)
I would have thought it would be round until 4.4
Also, if I use 4.55 to enter the format, 4.5 is output where I expect 4.6 Is there an error with java or with my understanding?
source share