1 year ago

#358877

test-img

flitzpiepe96

DecimalFormat rounding issue?

Am I doing something terribly wrong or does DecimalFormat really have an issue with rounding?

DecimalFormat format = new DecimalFormat("#.####");
format.setRoundingMode(RoundingMode.HALF_UP); // .5 up, .4 down?
format.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH)); // . as decimal seperator

System.out.println(format.format(1.848751)); // 1.8488 -> thats correct
System.out.println(format.format(1.848750)); // 1.8487 -> that is not correct
System.out.println(format.format(1.84875)); // 1.8487 -> that is not correct

java

rounding

java-11

decimalformat

0 Answers

Your Answer

Accepted video resources