ConvertUtil { @NonNull static String formatInt(double value, int precision) { if (value < 0) return "-" + formatInt(-value, precision); if (precision <= 0) precision = 1; final int valueInt = (int) Math.floor(value); final int mag = (int) Math.floor(Math.log10(value)); if (mag < precision) { return Integer.toString(valueInt); } else { final int mask = (int) Math.pow(10, mag - precision + 1); final int truncated = valueInt - (valueInt % mask); return Integer.toString(truncated); } }    }
@Test public void convertFormatInt() { assertEquals( "9", ConvertUtil.formatInt(9.8, 0)); assertEquals( "9", ConvertUtil.formatInt(9.8, 1)); assertEquals( "9", ConvertUtil.formatInt(9.8, 2)); assertEquals( "9", ConvertUtil.formatInt(9.8, 3)); assertEquals( "90", ConvertUtil.formatInt(98.6, 1)); assertEquals( "98", ConvertUtil.formatInt(98.6, 2)); assertEquals( "98", ConvertUtil.formatInt(98.6, 3)); assertEquals( "-9", ConvertUtil.formatInt(-9.8, 1)); assertEquals( "-9", ConvertUtil.formatInt(-9.8, 2)); assertEquals( "-9", ConvertUtil.formatInt(-9.8, 3)); }