QuoteUtil { public static BigDecimal roundPrice(BigDecimal price, BigDecimal ticksize) { return roundPrice(price, ticksize, ticksize.scale()); }  static BigDecimal getBigDecimalValue(Ticker ticker, double value); static BigDecimal getBigDecimalValue(int value); static BigDecimal roundPrice(BigDecimal price, BigDecimal ticksize); static BigDecimal roundPrice(BigDecimal price, BigDecimal ticksize, int decimalPlaces);  }
@Test public void testRoundPrice() { BigDecimal stopLoss = getPrice( 1323.37, 2 ); BigDecimal tickSize = getPrice( 0.25, 2 ); BigDecimal rounded = QuoteUtil.roundPrice( stopLoss, tickSize, 2 ); System.out.println( "Rounded: " + rounded ); assertEquals( rounded, getPrice(1323.25, 2)); stopLoss = getPrice( 1323.38, 2 ); rounded = QuoteUtil.roundPrice( stopLoss, tickSize,2 ); System.out.println( "Rounded: " + rounded ); assertEquals( rounded, getPrice( 1323.50, 2 )); tickSize = getPrice(1,0); stopLoss = getPrice( 11100.54, 0 ); rounded = QuoteUtil.roundPrice( stopLoss, tickSize, 0 ); System.out.println( "Rounded: " + rounded ); assertEquals( rounded, getPrice( 11101, 0 )); stopLoss = getPrice( 11100.44, 0 ); rounded = QuoteUtil.roundPrice( stopLoss, tickSize, 0 ); System.out.println( "Rounded: " + rounded ); assertEquals( rounded, getPrice( 11100, 0 )); rounded = QuoteUtil.roundPrice( new BigDecimal(1323.3737373), new BigDecimal(0.25), 2 ); System.out.println( "Rounded: " + rounded ); assertEquals( rounded, getPrice(1323.25, 2)); }
@Test public void testRoundPriceUseTicksizeScale() { double value = 16.778; BigDecimal price = new BigDecimal(value).setScale(3, RoundingMode.HALF_UP); BigDecimal scale = new BigDecimal("0.01").setScale(2, RoundingMode.HALF_UP); BigDecimal actual = QuoteUtil.roundPrice(price, scale); BigDecimal expected = new BigDecimal(16.78).setScale(2, RoundingMode.HALF_UP); assertEquals( expected, actual ); }