Skip to content

Commit

Permalink
avg in sum builder
Browse files Browse the repository at this point in the history
  • Loading branch information
witek committed Apr 29, 2015
1 parent 625b42b commit 5f7dc15
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/ilarkesto/core/base/BigDecimalSumBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
public class BigDecimalSumBuilder implements Formatable {

private BigDecimal sum;
private int count;

public BigDecimalSumBuilder add(Number value) {
count++;
if (value == null) return this;
BigDecimal bdValue = ((value instanceof BigDecimal) ? (BigDecimal) value : new BigDecimal(value.toString()));
if (sum == null) {
Expand All @@ -38,6 +40,10 @@ public BigDecimal getSum() {
return sum;
}

public BigDecimal getAvg(int scale) {
return sum.divide(new BigDecimal(count), scale, BigDecimal.ROUND_HALF_UP);
}

@Override
public String format() {
return sum == null ? null : Localizer.get().format(sum, true, 2);
Expand Down

0 comments on commit 5f7dc15

Please sign in to comment.