FormatUtil { public static float round(float d, int decimalPlace) { final BigDecimal bd = new BigDecimal(Float.toString(d)).setScale(decimalPlace, RoundingMode.HALF_UP); return bd.floatValue(); } private  FormatUtil(); static String formatBytes(long bytes); static String formatBytesDecimal(long bytes); static String formatHertz(long hertz); static String formatValue(long value, String unit); static String formatElapsedSecs(long secs); static float round(float d, int decimalPlace); static long getUnsignedInt(int x); static String toUnsignedString(int i); static String toUnsignedString(long l); static String formatError(int errorCode); static final String HEX_ERROR; }
@Test public void testRound() { assertEquals(42.42, FormatUtil.round(42.423f, 2), 0.00001f); assertEquals(42.43, FormatUtil.round(42.425f, 2), 0.00001f); assertEquals(42.5, FormatUtil.round(42.499f, 2), 0.00001f); assertEquals(42, FormatUtil.round(42, 2), 0.00001f); }