String.format in Java has weird rounding by default

I was participating in one of our Android projects, and while testing I discovered, that our program acted weird — app displayed one value, but acted like the value was different. The value was 1.45, and our Java Android app displayed 1.4 instead of 1.5.

The source looked something like:
double d = 1.45;
String val = String.format(Locale.US, "%.1f", d) + "%";

I am still a beginner in Java, and at first I believed, that String.format rounds down by default. This was till I saw, that value 1.55 is rounded up by default.

Digging into Java documentation I found, that String.format uses RoundingMode HALF_EVEN by default:

Rounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for RoundingMode.HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for RoundingMode.HALF_DOWN if it’s even. Note that this is the rounding mode that statistically minimizes cumulative error when applied repeatedly over a sequence of calculations. It is sometimes known as “Banker’s rounding,” and is chiefly used in the USA. This rounding mode is analogous to the rounding policy used for float and double arithmetic in Java.

http://docs.oracle.com/javase/6/docs/api/java/math/RoundingMode.html

Of course other beginners are also discovering this Java Rounding.

Unfortunately Android does not support setRoundingMode, so currently using the following workaround:
double d = (double)Math.round(((double)percents) * 10f) / 10f;
String val = String.format(Locale.US, "%.1f", d) + "%";

One reply on “String.format in Java has weird rounding by default”

  1. Rounding mode to round towards zero. Never increments the digit prior to a discarded fraction (i.e., truncates). Note that this rounding mode never increases the magnitude of the calculated value.

Comments are closed.