StringBuilderUtil { public static void format3f(@NonNull StringBuilder sb, double x) { final long x1000 = Math.round(x * 1000); final long whole = x1000 / 1000; final long tenths = x1000 / 100 % 10; final long hundredths = x1000 / 10 % 10; final long thousandths = x1000 % 10; sb.append(whole); sb.append("."); sb.append(tenths); sb.append(hundredths); sb.append(thousandths); }  static void format2f(@NonNull StringBuilder sb, float x); static void format3f(@NonNull StringBuilder sb, double x); static void format2d(@NonNull StringBuilder sb, long x); static void format3d(@NonNull StringBuilder sb, long x);  }
@Test public void format3f() { StringBuilder sb = new StringBuilder(); for (double value : testDoubles) { sb.setLength(0); StringBuilderUtil.format3f(sb, value); assertEquals(String.format("%.3f", value), sb.toString()); } }