@Test public void testRound() { Assert.assertEquals(ArithUtils.round(0.1234, 2), 0.12, 10); Assert.assertEquals(ArithUtils.round(0.0049, 2), 0.00, 10); Assert.assertEquals(ArithUtils.round(0.0050, 2), 0.01, 10); Assert.assertEquals(ArithUtils.round(0.0099, 2), 0.01, 10); Assert.assertEquals(ArithUtils.round(0.0000, 2), 0.00, 10); Assert.assertEquals(ArithUtils.round(-0.1234, 2), -0.12, 10); Assert.assertEquals(ArithUtils.round(1234567890.1234, 2), 1234567890.12, 10); Assert.assertEquals(ArithUtils.round(-1234567890.1234, 2), -1234567890.12, 10); }
public static Double round(Double v, int scale) { if (v == null) { return null; } if (scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); }
ArithUtils { public static Double round(Double v, int scale) { if (v == null) { return null; } if (scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } }
ArithUtils { public static Double round(Double v, int scale) { if (v == null) { return null; } if (scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } private  ArithUtils(); }
ArithUtils { public static Double round(Double v, int scale) { if (v == null) { return null; } if (scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } private  ArithUtils(); static Double add(Double v1, Double v2); static Double sub(Double v1, Double v2); static Double mul(Double v1, Double v2); static Double div(Double v1, Double v2); static Double div(Double v1, Double v2, int scale); static Double round(Double v, int scale); }
ArithUtils { public static Double round(Double v, int scale) { if (v == null) { return null; } if (scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } private  ArithUtils(); static Double add(Double v1, Double v2); static Double sub(Double v1, Double v2); static Double mul(Double v1, Double v2); static Double div(Double v1, Double v2); static Double div(Double v1, Double v2, int scale); static Double round(Double v, int scale);  }