EventUtils { public static String prettifyNs(long ns) { BigDecimal bd = new BigDecimal(ns); String uom = "ns"; while (bd.subtract(BigDecimal.valueOf(999)).compareTo(BigDecimal.ONE) >= 0 && !uom.equals("s")) { bd = bd.divide(new BigDecimal(1000), new MathContext(20, RoundingMode.FLOOR)); switch (uom) { case "ns": uom = "us"; break; case "us": uom = "ms"; break; case "ms": uom = "s"; break; } } bd = bd.setScale(2, RoundingMode.FLOOR); bd = bd.stripTrailingZeros(); return bd.toPlainString() + uom; }  static String prettifyNs(long ns); static String getCallingClassName();  }
@Test public void testPrettifyNs() { assertEquals("1ns", EventUtils.prettifyNs(1)); assertEquals("100ns", EventUtils.prettifyNs(100)); assertEquals("123ns", EventUtils.prettifyNs(123)); assertEquals("999ns", EventUtils.prettifyNs(999)); assertEquals("1us", EventUtils.prettifyNs(1000)); assertEquals("100us", EventUtils.prettifyNs(100000)); assertEquals("123.45us", EventUtils.prettifyNs(123456)); assertEquals("999.99us", EventUtils.prettifyNs(999999)); assertEquals("1ms", EventUtils.prettifyNs(1000000)); assertEquals("100ms", EventUtils.prettifyNs(100000000)); assertEquals("123.45ms", EventUtils.prettifyNs(123456789)); assertEquals("999.99ms", EventUtils.prettifyNs(999999999)); assertEquals("1s", EventUtils.prettifyNs(1000000000)); assertEquals("100s", EventUtils.prettifyNs(100000000000L)); assertEquals("123.45s", EventUtils.prettifyNs(123456789012L)); assertEquals("999.99s", EventUtils.prettifyNs(999999999999L)); }