List of usage examples for java.math MathContext getRoundingMode
public RoundingMode getRoundingMode()
From source file:com.qcadoo.model.internal.units.UnitConversionImpl.java
private UnitConversionImpl(final String unitFrom, final String unitTo, final BigDecimal ratio, final MathContext mathContext) { Preconditions.checkNotNull(unitFrom); Preconditions.checkNotNull(ratio);//from w w w.j av a2 s . c o m Preconditions.checkNotNull(mathContext); this.unitFrom = unitFrom; this.unitTo = unitTo; this.ratio = ratio.setScale(mathContext.getPrecision(), mathContext.getRoundingMode()); this.mathContext = mathContext; }
From source file:dk.clanie.money.Money.java
/** * Divide evenly into parts./*from w w w .ja v a2s . c om*/ * * Divides an amount into parts of approximately the same size while * ensuring that the sum of all the parts equals the whole. * <p> * Parts of unequal size will be distributed evenly in the returned array. * <p> * For example, if asked to divede 20 into 6 parts the result will be {3.33, * 3.34, 3.33, 3.33, 3.34, 3.33} * * @param parts - * number of parts * @return Money[] with the parts */ public Money[] divideEvenlyIntoParts(int parts) { Money[] res = new Money[parts]; final BigDecimal bdParts = new BigDecimal(parts); final MathContext mc = MathContext.DECIMAL128; BigDecimal sumOfPreviousParts = BigDecimal.ZERO; for (int i = 0; i < parts; i++) { Money part = new Money(); BigDecimal sumOfParts = amount.multiply(new BigDecimal(i + 1)); sumOfParts = sumOfParts.divide(bdParts, mc); sumOfParts = sumOfParts.setScale(amount.scale(), mc.getRoundingMode()); part.amount = sumOfParts.subtract(sumOfPreviousParts); sumOfPreviousParts = sumOfParts; part.currency = currency; res[i] = part; } return res; }
From source file:com.premiumminds.billy.core.services.builders.impl.GenericInvoiceBuilderImpl.java
protected void validateValues() throws ValidationException { GenericInvoiceEntity i = this.getTypeInstance(); i.setCurrency(Currency.getInstance("EUR")); // FIXME: Hardcoded currency. // Blocks usage of any other // currency MathContext mc = BillyMathContext.get(); BigDecimal amountWithTax = BigDecimal.ZERO; BigDecimal taxAmount = BigDecimal.ZERO; BigDecimal amountWithoutTax = BigDecimal.ZERO; for (GenericInvoiceEntry e : this.getTypeInstance().getEntries()) { amountWithTax = amountWithTax.add(e.getUnitAmountWithTax().multiply(e.getQuantity(), mc), mc); taxAmount = taxAmount.add(e.getUnitTaxAmount().multiply(e.getQuantity(), mc), mc); amountWithoutTax = amountWithoutTax.add(e.getUnitAmountWithoutTax().multiply(e.getQuantity(), mc), mc); if (e.getCurrency() == null) { GenericInvoiceEntryEntity entry = (GenericInvoiceEntryEntity) e; entry.setCurrency(i.getCurrency()); e = entry;/* www .ja va2 s . co m*/ } else { Validate.isTrue(i.getCurrency().getCurrencyCode().equals(e.getCurrency().getCurrencyCode())); } } i.setAmountWithTax(amountWithTax); i.setTaxAmount(taxAmount); i.setAmountWithoutTax(amountWithoutTax); Validate.isTrue( i.getAmountWithTax().subtract(i.getTaxAmount(), mc).setScale(7, mc.getRoundingMode()) .compareTo(i.getAmountWithoutTax().setScale(7, mc.getRoundingMode())) == 0, "The invoice values are invalid", // TODO message i.getAmountWithTax(), i.getAmountWithoutTax(), i.getTaxAmount()); Validate.isTrue( i.getAmountWithTax().compareTo(BigDecimal.ZERO) > 0 && i.getAmountWithoutTax().compareTo(BigDecimal.ZERO) >= 0 && i.getTaxAmount().compareTo(BigDecimal.ZERO) >= 0, "The invoice values are lower than zero", // TODO // message i.getAmountWithTax(), i.getAmountWithoutTax(), i.getTaxAmount()); }