Java Code Examples for javax.money.MonetaryAmount#with()

The following examples show how to use javax.money.MonetaryAmount#with() . 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: JavaMoneyUnitManualTest.java    From tutorials with MIT License 7 votes vote down vote up
@Test
@Ignore("Currency providers are not always available")
public void givenAmount_whenConversion_thenNotNull() {
    MonetaryAmount oneDollar = Monetary
      .getDefaultAmountFactory()
      .setCurrency("USD")
      .setNumber(1)
      .create();

    CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR");

    MonetaryAmount convertedAmountUSDtoEUR = oneDollar.with(conversionEUR);

    assertEquals("USD 1", oneDollar.toString());
    assertNotNull(convertedAmountUSDtoEUR);
}
 
Example 2
Source File: JavaMoneyUnitManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenAmount_whenRounded_thanEquals() {
    MonetaryAmount fstAmtEUR = Monetary
      .getDefaultAmountFactory()
      .setCurrency("EUR")
      .setNumber(1.30473908)
      .create();
    MonetaryAmount roundEUR = fstAmtEUR.with(Monetary.getDefaultRounding());
    assertEquals("EUR 1.30473908", fstAmtEUR.toString());
    assertEquals("EUR 1.3", roundEUR.toString());
}
 
Example 3
Source File: MonetaryOperatorsTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#reciprocal()}.
 */
@Test
public void testReciprocal() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency("CHF").setNumber(200).create();
	MonetaryAmount r = m.with(MonetaryOperators.reciprocal());
       //noinspection BigDecimalMethodWithoutRoundingCalled
       assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency("CHF")
					.setNumber(BigDecimal.ONE.divide(BigDecimal.valueOf(200)))
					.create(),
			r);
}
 
Example 4
Source File: MonetaryOperatorsTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#permil(java.math.BigDecimal)}
 * .
 */
@Test
public void testPermilBigDecimal() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency(
					"CHF").setNumber(100).create();
	MonetaryAmount r = m.with(MonetaryOperators.permil(BigDecimal
			.valueOf(25)));
	assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency("CHF")
					.setNumber(
							new BigDecimal("2.5")).create(),
			r);
}
 
Example 5
Source File: MonetaryOperatorsTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#permil(java.lang.Number)}
 * .
 */
@Test
public void testPermilNumber() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency(
					"CHF").setNumber(100).create();
	MonetaryAmount r = m.with(MonetaryOperators.permil(25));
	assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency("CHF")
					.setNumber(
							new BigDecimal("2.5")).create(),
			r);
}
 
Example 6
Source File: MonetaryOperatorsTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#permil(java.lang.Number, java.math.MathContext)}
 * .
 */
@Test
public void testPermilNumberMathContext() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency(
					"CHF").setNumber(100).create();
	MonetaryAmount r = m.with(MonetaryOperators.permil(25,
			MathContext.DECIMAL64));
	assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency("CHF")
					.setNumber(
							new BigDecimal("2.5")).create(),
			r);
}
 
Example 7
Source File: MonetaryOperatorsTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#percent(java.math.BigDecimal)}
 * .
 */
@Test
public void testPercentBigDecimal() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency(
					"CHF").setNumber(100L).create();
	MonetaryAmount r = m.with(MonetaryOperators.percent(BigDecimal
			.valueOf(25)));
	assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency("CHF")
					.setNumber(
							new BigDecimal("25")).create(),
			r);
}
 
Example 8
Source File: MonetaryOperatorsTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#percent(java.lang.Number)}
 * .
 */
@Test
public void testPercentNumber() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency(
					"CHF").setNumber(100).create();
	MonetaryAmount r = m.with(MonetaryOperators.percent((long) 25));
	assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency("CHF")
					.setNumber(
							new BigDecimal("25")).create(),
			r);
}
 
Example 9
Source File: MonetaryOperatorsTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#minorPart()}.
 */
@Test
public void testMinorPart() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency(
					"CHF").setNumber(new BigDecimal(
					"1234.56789")).create();
	MonetaryAmount r = m.with(MonetaryOperators.minorPart());
	assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency("CHF")
					.setNumber(
							new BigDecimal("0.56789")).create(),
			r);
}
 
Example 10
Source File: MonetaryOperatorsTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#majorPart()}.
 */
@Test
public void testMajorPart() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency(
					"CHF").setNumber(new BigDecimal(
					"1234.56789")).create();
	MonetaryAmount r = m.with(MonetaryOperators.majorPart());
	assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency(
					"CHF").setNumber(new BigDecimal("1234")).create(),
			r);
}
 
Example 11
Source File: MonetaryConversionTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonetaryConversionsECB() {
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion("CHF", "ECB");
	MonetaryAmount converted = amt.with(conv);
	assertNotNull(converted);
}
 
Example 12
Source File: MonetaryConversionTest.java    From jsr354-ri 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 13
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonetaryConversionsECB() {
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion("CHF", "ECB");
	MonetaryAmount converted = amt.with(conv);
	assertNotNull(converted);
}
 
Example 14
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 15
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonetaryConversionsECB() {
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion("CHF", "ECB");
	MonetaryAmount converted = amt.with(conv);
	assertNotNull(converted);
}
 
Example 16
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);
}