/** * Returns a copy of this monetary value with the specified amount. * <p> * The returned instance will have this currency and the new amount. * If the scale of the {@code BigDecimal} needs to be adjusted, then * it will be rounded using the specified mode. * <p> * This instance is immutable and unaffected by this method. * * @param amount the monetary amount to set in the returned instance, not null * @param roundingMode the rounding mode to adjust the scale, not null * @return the new instance with the input amount set, never null */ public Money withAmount(BigDecimal amount, RoundingMode roundingMode) { return with(money.withAmount(amount).withCurrencyScale(roundingMode)); }
/** * Returns a copy of this monetary value converted into another currency * using the specified conversion rate, with a rounding mode used to adjust * the decimal places in the result. * <p> * This instance is immutable and unaffected by this method. * * @param currency the new currency, not null * @param conversionMultipler the conversion factor between the currencies, not null * @param roundingMode the rounding mode to use to bring the decimal places back in line, not null * @return the new multiplied instance, never null * @throws IllegalArgumentException if the currency is the same as this currency * @throws IllegalArgumentException if the conversion multiplier is negative * @throws ArithmeticException if the rounding fails */ public Money convertedTo(CurrencyUnit currency, BigDecimal conversionMultipler, RoundingMode roundingMode) { return with(money.convertedTo(currency, conversionMultipler).withCurrencyScale(roundingMode)); }
/** * Returns a copy of this monetary value with the specified currency. * <p> * The returned instance will have the specified currency and the amount * from this instance. If the number of decimal places differs between the * currencies, then the amount may be rounded. * <p> * This instance is immutable and unaffected by this method. * * @param currency the currency to use, not null * @param roundingMode the rounding mode to use to bring the decimal places back in line, not null * @return the new instance with the input currency set, never null * @throws ArithmeticException if the rounding fails */ public Money withCurrencyUnit(CurrencyUnit currency, RoundingMode roundingMode) { return with(money.withCurrencyUnit(currency).withCurrencyScale(roundingMode)); }
@Override protected BigMoney fromConvertedColumns(Object[] convertedColumns) { CurrencyUnit currencyUnitPart = (CurrencyUnit) convertedColumns[0]; Long amountMajorPart = (Long) convertedColumns[1]; BigMoney theMoney = BigMoney.ofMajor(currencyUnitPart, amountMajorPart); if (theMoney.getScale() < currencyUnitPart.getDecimalPlaces()) { theMoney = theMoney.withCurrencyScale(); } return theMoney; }
/** * Returns a copy of this monetary value with the specified amount using a well-defined * conversion from a {@code double}. * <p> * The returned instance will have this currency and the new amount. * If the scale of the {@code BigDecimal} needs to be adjusted, then * it will be rounded using the specified mode. * <p> * The amount is converted via {@link BigDecimal#valueOf(double)} which yields * the most expected answer for most programming scenarios. * Any {@code double} literal in code will be converted to * exactly the same BigDecimal with the same scale. * For example, the literal '1.45d' will be converted to '1.45'. * <p> * This instance is immutable and unaffected by this method. * * @param amount the monetary amount to set in the returned instance, not null * @param roundingMode the rounding mode to adjust the scale, not null * @return the new instance with the input amount set, never null */ public Money withAmount(double amount, RoundingMode roundingMode) { return with(money.withAmount(amount).withCurrencyScale(roundingMode)); }
@Override public BigMoney fromNonNullValue(Long value) { BigMoney theMoney = BigMoney.ofMajor(currencyUnit, value); if (theMoney.getScale() < currencyUnit.getDecimalPlaces()) { theMoney = theMoney.withCurrencyScale(); } return theMoney; }
@Override protected BigMoney fromConvertedColumns(Object[] convertedColumns) { CurrencyUnit currencyUnitPart = (CurrencyUnit) convertedColumns[0]; Long amountMajorPart = (Long) convertedColumns[1]; BigMoney theMoney = BigMoney.ofMajor(currencyUnitPart, amountMajorPart); if (theMoney.getScale() < currencyUnitPart.getDecimalPlaces()) { theMoney = theMoney.withCurrencyScale(); } return theMoney; }
/** * Obtains an instance of {@code Money} from a provider, rounding as necessary. * <p> * This allows you to create an instance from any class that implements the * provider, such as {@code BigMoney}. * The rounding mode is used to adjust the scale to the scale of the currency. * * @param moneyProvider the money to convert, not null * @param roundingMode the rounding mode to use, not null * @return the new instance, never null * @throws ArithmeticException if the rounding fails */ public static Money of(BigMoneyProvider moneyProvider, RoundingMode roundingMode) { MoneyUtils.checkNotNull(moneyProvider, "BigMoneyProvider must not be null"); MoneyUtils.checkNotNull(roundingMode, "RoundingMode must not be null"); return new Money(BigMoney.of(moneyProvider).withCurrencyScale(roundingMode)); }