@Test() public void testGetWeightedAverage1() { Double d1 = 150d; Double d2 = 149d; Long q1 = 0L; Long q2 = 1L; Double result = MoneyUtil.getWeightedAverage(d1, q1, d2, q2); assertEquals(149d, result.doubleValue(), 2); }
public static Double getWeightedAverage(Map<Double, Long> amounts) { Double totalAmount = 0d; Long quantity = 0L; if (amounts != null && amounts.size() > 0) { for (Double money : amounts.keySet()) { Double amount = ArithUtils.mul(money, amounts.get(money).doubleValue()); totalAmount = ArithUtils.add(totalAmount, amount); quantity += amounts.get(money); } } return ArithUtils.div(totalAmount, quantity.doubleValue()); }
MoneyUtil { public static Double getWeightedAverage(Map<Double, Long> amounts) { Double totalAmount = 0d; Long quantity = 0L; if (amounts != null && amounts.size() > 0) { for (Double money : amounts.keySet()) { Double amount = ArithUtils.mul(money, amounts.get(money).doubleValue()); totalAmount = ArithUtils.add(totalAmount, amount); quantity += amounts.get(money); } } return ArithUtils.div(totalAmount, quantity.doubleValue()); } }
MoneyUtil { public static Double getWeightedAverage(Map<Double, Long> amounts) { Double totalAmount = 0d; Long quantity = 0L; if (amounts != null && amounts.size() > 0) { for (Double money : amounts.keySet()) { Double amount = ArithUtils.mul(money, amounts.get(money).doubleValue()); totalAmount = ArithUtils.add(totalAmount, amount); quantity += amounts.get(money); } } return ArithUtils.div(totalAmount, quantity.doubleValue()); }  }
MoneyUtil { public static Double getWeightedAverage(Map<Double, Long> amounts) { Double totalAmount = 0d; Long quantity = 0L; if (amounts != null && amounts.size() > 0) { for (Double money : amounts.keySet()) { Double amount = ArithUtils.mul(money, amounts.get(money).doubleValue()); totalAmount = ArithUtils.add(totalAmount, amount); quantity += amounts.get(money); } } return ArithUtils.div(totalAmount, quantity.doubleValue()); }  static boolean equals(Double money1, Double money2); static Double fixPrice(Double price); static Double fixDiscount(Double discount); static Double getWeightedAverage(Map<Double, Long> amounts); static Double getWeightedAverage(Double money1, Long quantity1, Double money2, Long quantity2); }
MoneyUtil { public static Double getWeightedAverage(Map<Double, Long> amounts) { Double totalAmount = 0d; Long quantity = 0L; if (amounts != null && amounts.size() > 0) { for (Double money : amounts.keySet()) { Double amount = ArithUtils.mul(money, amounts.get(money).doubleValue()); totalAmount = ArithUtils.add(totalAmount, amount); quantity += amounts.get(money); } } return ArithUtils.div(totalAmount, quantity.doubleValue()); }  static boolean equals(Double money1, Double money2); static Double fixPrice(Double price); static Double fixDiscount(Double discount); static Double getWeightedAverage(Map<Double, Long> amounts); static Double getWeightedAverage(Double money1, Long quantity1, Double money2, Long quantity2);  }