Java Code Examples for org.javamoney.moneta.RoundedMoney#of()

The following examples show how to use org.javamoney.moneta.RoundedMoney#of() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MonetaryFormatsTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
/**
 * Tests formatting and parsing back the values using all available locales.
 */
@Test
public void testRoundRobinForAllLocales_RoundedMoney(){
    String report = "";
    Locale defaultLocale = Locale.getDefault();
    try {
        for (Locale locale : Locale.getAvailableLocales()) {
            Locale.setDefault(locale);
            try {
                RoundedMoney money = RoundedMoney.of(1.2, "EUR");
                if (!money.equals(RoundedMoney.parse(money.toString()))) {
                    report += "FAILED : " + locale + "(" + money.toString() + ")\n";
                } else {
                    report += "SUCCESS: " + locale + "\n";
                }
            }catch(Exception e){
                report += "ERROR: " + locale + " -> " + e + "\n";
            }
        }
        assertFalse(report.contains("FAILED"),"Formatting and parsing failed for some locales:\n\n"+report);
    }finally{
        Locale.setDefault(defaultLocale);
    }
}
 
Example 2
Source File: MoneyModule.java    From jackson-datatype-money with MIT License 5 votes vote down vote up
/**
 * @see RoundedMoney
 * @param rounding the rounding operator
 * @return new {@link MoneyModule} using {@link RoundedMoney} with the given {@link MonetaryRounding}
 */
public MoneyModule withRoundedMoney(final MonetaryOperator rounding) {
    final MonetaryAmountFactory<RoundedMoney> factory = (amount, currency) ->
            RoundedMoney.of(amount, currency, rounding);

    return new MoneyModule(writer, names, formatFactory, factory,
            fastMoneyFactory, moneyFactory, factory);
}
 
Example 3
Source File: ScaleRoundedOperator.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
	RoundedMoney roundedMoney = RoundedMoney.from(Objects.requireNonNull(amount));
	BigDecimal numberValue = roundedMoney.getNumber().numberValue(BigDecimal.class);
	BigDecimal numberRounded = numberValue.setScale(scale, roundingMode);
	return RoundedMoney.of(numberRounded, roundedMoney.getCurrency(), this);
}
 
Example 4
Source File: PrecisionContextRoundedOperator.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
	RoundedMoney roundedMoney = RoundedMoney.from(Objects.requireNonNull(amount));
	BigDecimal numberValue = roundedMoney.getNumber().numberValue(BigDecimal.class);
	BigDecimal numberRounded = numberValue.round(mathContext);
	return RoundedMoney.of(numberRounded, roundedMoney.getCurrency(), this);
}
 
Example 5
Source File: RoundedMoneyProducer.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public MonetaryAmount create(CurrencyUnit currency, Number number) {
	return RoundedMoney.of(Objects.requireNonNull(number), Objects.requireNonNull(currency), operator);
}
 
Example 6
Source File: DefaultMonetaryRoundedFactory.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public MonetaryAmount create(Number number, CurrencyUnit currencyUnit) {
	return RoundedMoney.of(requireNonNull(number), requireNonNull(currencyUnit), roundingOperator);
}
 
Example 7
Source File: RoundedMoneyProducer.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public MonetaryAmount create(CurrencyUnit currency, Number number) {
	return RoundedMoney.of(Objects.requireNonNull(number), Objects.requireNonNull(currency), operator);
}
 
Example 8
Source File: RoundedMoneyAmountFactory.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
protected RoundedMoney create(Number number, CurrencyUnit currency, MonetaryContext monetaryContext) {
    return RoundedMoney.of(number, currency, MonetaryContext.from(monetaryContext, RoundedMoney.class));
}