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

The following examples show how to use org.javamoney.moneta.Money#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: FutureValueGrowingAnnuityTest.java    From javamoney-lib with Apache License 2.0 6 votes vote down vote up
/**
 * Calculate periods n.
 *
 * @throws Exception the exception
 */
@Test
public void calculate_PeriodsN() throws Exception {
    Money m = Money.of(10, "CHF");
    FutureValueGrowingAnnuity val = FutureValueGrowingAnnuity.of(
            Rate.of(-0.05), Rate.of(0.05), 10
    );
    assertEquals(Money.of(0.0,"CHF").getNumber().numberValue(BigDecimal.class)
            .doubleValue(), m.with(val).getNumber().numberValue(BigDecimal.class)
            .doubleValue(), 0.00000000000001d);
    val = FutureValueGrowingAnnuity.of(
            Rate.of(0.05), Rate.of(-0.05), 10
    );
    assertEquals(Money.of(103.0157687539062,"CHF").getNumber().numberValue(BigDecimal.class).doubleValue(),
            m.with(val).getNumber().numberValue(BigDecimal.class).doubleValue(), 0.000000000000001d);
}
 
Example 2
Source File: FormattingLocaleDemo.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	final String COMPARISON = "EUR 123,456.78";
		
	MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(Locale.ROOT);

	MonetaryAmount source = Money.of(123456.78, "EUR");
	String formatted = format.format(source);

	System.out.println(formatted); // "EUR 123,456.78", space is a 32 on JDK 8, a 160 on JDK 9 and 10
	System.out.println((int) formatted.toCharArray()[3]); // JDK 8: 32, JDK 9: 160, JDK 10: 160

	MonetaryAmount result = format.parse(COMPARISON); // Space is char of 32 (standard space)
	formatted = format.format(result);
	System.out.println(formatted);
	System.out.println(COMPARISON.equals(formatted));
	System.out.println(result.toString());
}
 
Example 3
Source File: StreamFactory.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
public static  Stream<MonetaryAmount> streamNull() {
    Money m1 = Money.of(BigDecimal.TEN, BRAZILIAN_REAL);
    Money m2 = Money.of(BigDecimal.ZERO, BRAZILIAN_REAL);
    Money m3 = Money.of(BigDecimal.ONE, BRAZILIAN_REAL);
    Money m4 = Money.of(BigDecimal.valueOf(4L), BRAZILIAN_REAL);
    Money m5 = Money.of(BigDecimal.valueOf(5L), BRAZILIAN_REAL);
    return Stream.of(m1, m2, m3, m4, m5, null);
}
 
Example 4
Source File: StreamFactory.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
public static Stream<MonetaryAmount> currencies() {
    Money r1 = Money.of(BigDecimal.TEN, BRAZILIAN_REAL);
    Money r2 = Money.of(BigDecimal.ZERO, BRAZILIAN_REAL);
    Money r3 = Money.of(BigDecimal.ONE, BRAZILIAN_REAL);

    Money e1 = Money.of(BigDecimal.TEN, EURO);
    Money e2 = Money.of(BigDecimal.ZERO, EURO);
    Money e3 = Money.of(BigDecimal.ONE, EURO);

    Money d1 = Money.of(BigDecimal.TEN, DOLLAR);
    Money d2 = Money.of(BigDecimal.ZERO, DOLLAR);
    Money d3 = Money.of(BigDecimal.ONE, DOLLAR);
    return Stream.of(r1, r2, r3, e1, e2, e3, d1, d2, d3);
}
 
Example 5
Source File: FutureValueOfAnnuityDueTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate periods 0.
 *
 * @throws Exception the exception
 */
@Test
public void calculate_Periods0() throws Exception {
    Money m = Money.of(10, "CHF");
    FutureValueOfAnnuityDue val = FutureValueOfAnnuityDue.of(
            RateAndPeriods.of(0.05, 0)
    );
    assertEquals(Money.of(0,"CHF"), m.with(val));
    val = FutureValueOfAnnuityDue.of(
            RateAndPeriods.of(-0.05, 0)
    );
    assertEquals(Money.of(0,"CHF"), m.with(val));
}
 
Example 6
Source File: TransactionFactory.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
/**
 * This method creates and returns a new transaction from the criteria
 * provided by the form. The specified key is used to determine the proper
 * amount; expenses are negative and incomes are positive.
 * 
 * @param type
 *            The type of transaction to create.
 * 
 * @return A new transaction.
 */
protected final Transaction createTransaction(TransactionTypeKeys type) {
	Transaction trans = null;
	String payee = getForm().getPayFrom();
	double amount = parseAmount();

	// Put amount in proper form.
	if ((type == INCOME && amount < 0.0)
			|| (type == EXPENSE && amount >= 0.0)) {
		amount = -amount;
	}

	// Put payee in proper form.
	payee = purgeIdentifier(payee);

	trans = new Transaction(getForm().getField(CHECK_NUMBER).getText(),
			parseDate(), payee, Money.of(amount,
					UI_CURRENCY_SYMBOL.getCurrency()), getCategory(),
			getForm().getField(NOTES).getText());

	// Set attributes not applicable in the constructor.
	trans.setIsReconciled(getForm().getButton(PENDING).isSelected() == false);

	if (isInEditMode() == true) {
		trans.setLabel(getEditModeTransaction().getLabel());
	}

	return trans;
}
 
Example 7
Source File: SmokeTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateMoney() {
    // Creating one
    Money amount1 = Money.of(1.0d, "CHF");
    Money amount2 = Money.of(1.0d, "CHF");
    Money amount3 = amount1.add(amount2);
    logger.debug(amount1 + " + " + amount2 + " = " + amount3);
    assertEquals(1.0d, amount1.getNumber().doubleValue(), 0);
    assertEquals(1.0d, amount2.getNumber().doubleValue(), 0);
    assertEquals(2.0d, amount3.getNumber().doubleValue(), 0);
}
 
Example 8
Source File: ECBCurrentRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsSameEuroValue() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(EURO);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, EURO);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), EURO);
    assertEquals(result.getNumber().numberValue(BigDecimal.class),
            BigDecimal.TEN);

}
 
Example 9
Source File: YahooRateProviderTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsSameBrazilianValue() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(BRAZILIAN_REAL);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, BRAZILIAN_REAL);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), BRAZILIAN_REAL);
    assertEquals(result.getNumber().numberValue(BigDecimal.class),
            BigDecimal.TEN);

}
 
Example 10
Source File: YahooRateProviderTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsSameDollarValue() {
    CurrencyConversion currencyConversion = provider.getCurrencyConversion(DOLLAR);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), DOLLAR);
    assertEquals(result.getNumber().numberValue(BigDecimal.class),
            BigDecimal.TEN);

}
 
Example 11
Source File: PresentValueOfAnnuityDueTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate periods 1.
 *
 * @throws Exception the exception
 */
@Test
public void calculate_Periods1() throws Exception {
    Money m = Money.of(100, "CHF");
    PresentValueOfAnnuityDue val = PresentValueOfAnnuityDue.of(
            RateAndPeriods.of(0.05, 1)
    );
    assertEquals(Money.of(100,"CHF"), m.with(val));
    val = PresentValueOfAnnuityDue.of(
            RateAndPeriods.of(-0.05, 1)
    );
    assertEquals(Money.of(100,"CHF"), m.with(val));
}
 
Example 12
Source File: NumericRepresentationTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetFractionDenominator() throws Exception{
    BigDecimal bd = new BigDecimal("0.1234523");
    MonetaryAmount amount = Money.of(bd, "USD");
    assertEquals("Invalid fraction denominator for " + bd, amount.getNumber().getAmountFractionDenominator(),
                 np.getFractionDenominator(amount));
}
 
Example 13
Source File: IMFRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertsDollarToBrazilian() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(BRAZILIAN_REAL);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), BRAZILIAN_REAL);
    assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example 14
Source File: PresentValueOfAnnuityTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate periods 0.
 *
 * @throws Exception the exception
 */
@Test
public void calculate_Periods0() throws Exception {
    Money m = Money.of(10, "CHF");
    PresentValueOfAnnuity val = PresentValueOfAnnuity.of(
            RateAndPeriods.of(0.05, 0)
    );
    assertEquals(Money.of(0,"CHF"), m.with(val));
    val = PresentValueOfAnnuity.of(
            RateAndPeriods.of(-0.05, 0)
    );
    assertEquals(Money.of(0,"CHF"), m.with(val));
}
 
Example 15
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonetaryConversionsIMF() {
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion("CHF", "IMF");
	MonetaryAmount converted = amt.with(conv);
	assertNotNull(converted);
}
 
Example 16
Source File: JavaMoneyUnitManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenCurrencies_whenCompared_thanNotequal() {
    MonetaryAmount oneDolar = Monetary
      .getDefaultAmountFactory()
      .setCurrency("USD")
      .setNumber(1)
      .create();
    Money oneEuro = Money.of(1, "EUR");

    assertFalse(oneEuro.equals(FastMoney.of(1, "EUR")));
    assertTrue(oneDolar.equals(Money.of(1, "USD")));
}
 
Example 17
Source File: YahooRateProviderTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertsBrazilianToDollar() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(DOLLAR);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, BRAZILIAN_REAL);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), DOLLAR);
    assertTrue(result.getNumber().doubleValue() > 0);

}
 
Example 18
Source File: ECBHistoricRateProviderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsSameEuroValue() {
    CurrencyConversion currencyConversion = provider
            .getCurrencyConversion(EURO);
    assertNotNull(currencyConversion);
    MonetaryAmount money = Money.of(BigDecimal.TEN, EURO);
    MonetaryAmount result = currencyConversion.apply(money);

    assertEquals(result.getCurrency(), EURO);
    assertEquals(result.getNumber().numberValue(BigDecimal.class),
            BigDecimal.TEN);

}
 
Example 19
Source File: ExchangeBean.java    From javamoney-examples with Apache License 2.0 4 votes vote down vote up
public MonetaryAmount getMounetaryFrom() {
	return Money.of(value, getCurrencyFrom());
}
 
Example 20
Source File: PersistentMoneyMinorAmountAndCurrency.java    From jadira with Apache License 2.0 3 votes vote down vote up
@Override
protected Money fromConvertedColumns(Object[] convertedColumns) {

    CurrencyUnit currencyUnitPart = (CurrencyUnit) convertedColumns[0];
    Long amountMinorPart = (Long) convertedColumns[1];

    BigDecimal majorVal = BigDecimal.valueOf(amountMinorPart, currencyUnitPart.getDefaultFractionDigits());

    return Money.of(majorVal, currencyUnitPart);
}