javax.money.Monetary Java Examples

The following examples show how to use javax.money.Monetary. 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: CurrenciesTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
/**
 * Test equals and hashCode methods for
 * {@link javax.money.CurrencyUnit}s.
 */
@Test
public void testEqualsHashCode() {
	String currencyCode = "USD";
	CurrencyUnit cur1 = Monetary.getCurrency(currencyCode, "default");
	CurrencyUnit cur2 = CurrencyUnitBuilder.of(currencyCode, "equals-hashCode-test")
               .setDefaultFractionDigits(2)
               .build(false);

	assertNotSame(cur1, cur2);
	assertNotEquals(cur1.getContext().getProviderName(), cur2.getContext().getProviderName());
	assertEquals(cur1, cur2);
	assertEquals(cur2, cur1);
	assertEquals(cur1.hashCode(), cur2.hashCode());
	assertEquals(cur1.hashCode(), currencyCode.hashCode());
	assertEquals(cur2.hashCode(), currencyCode.hashCode());
}
 
Example #2
Source File: PresentValueContinuousCompoundingTest.java    From javamoney-lib with Apache License 2.0 6 votes vote down vote up
/**
 * Method: of(Rate rate, int periods)
 *
 * @throws Exception the exception
 */
@Test
public void testOfAndApply() throws Exception {
    Money money = Money.of(100, "CHF");
    MonetaryOperator rounding = Monetary.getRounding(RoundingQueryBuilder.of().setScale(2).set(RoundingMode.HALF_EVEN)
            .build());
    assertEquals(Money.of(BigDecimal.valueOf(95.12), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(0.05, 1))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(90.48), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(0.05, 2))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(46.3), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(0.07, 11))).with(rounding));

    assertEquals(Money.of(BigDecimal.valueOf(100.00), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(0.05, 0))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(100.00), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(-0.05, 0))).with(rounding));

    assertEquals(Money.of(BigDecimal.valueOf(105.13), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(-0.05, 1))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(110.52), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(-0.05, 2))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(215.98), "CHF"), money.with(PresentValueContinuousCompounding.of(RateAndPeriods.of(-0.07, 11))).with(rounding));
}
 
Example #3
Source File: FastMoneyTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for
 * {@link org.javamoney.moneta.FastMoney#getFactory()#setCurrency(javax.money.CurrencyUnit)} and {@link org
 * .javamoney.moneta.FastMoney#getFactory()#setNumber(java.lang.Number)}  .
 */
@Test
public void testWithCurrencyUnitNumber() {
    FastMoney[] moneys = new FastMoney[]{FastMoney.of(100, "CHF"), FastMoney.of(34242344, "USD"),
            FastMoney.of(23123213.435, "EUR"), FastMoney.of(-23123213.435, "USS"), FastMoney.of(-23123213, "USN"),
            FastMoney.of(0, "GBP")};
    FastMoney s = FastMoney.of(10, "XXX");
    MonetaryAmount[] moneys2 = new MonetaryAmount[]{
            s.getFactory().setCurrency(Monetary.getCurrency("CHF")).setNumber(100).create(),
            s.getFactory().setCurrency(Monetary.getCurrency("USD")).setNumber(34242344).create(),
            s.getFactory().setCurrency(Monetary.getCurrency("EUR"))
                    .setNumber(new BigDecimal("23123213.435")).create(),
            s.getFactory().setCurrency(Monetary.getCurrency("USS"))
                    .setNumber(new BigDecimal("-23123213.435")).create(),
            s.getFactory().setCurrency(Monetary.getCurrency("USN")).setNumber(-23123213).create(),
            s.getFactory().setCurrency(Monetary.getCurrency("GBP")).setNumber(0).create()};
    for (int i = 0; i < moneys.length; i++) {
        assertEquals(moneys[i], moneys2[i], "with(Number) failed.");
    }
}
 
Example #4
Source File: PrecisionScaleRoundedOperatorTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney() {
	int scale = 3;
	int precision = 5;

	CurrencyUnit real = Monetary.getCurrency("BRL");
	BigDecimal valueOf = BigDecimal.valueOf(35.34567);
	MonetaryAmount money = Money.of(valueOf, real);

	MathContext mathContext = new MathContext(precision, RoundingMode.HALF_EVEN);
	PrecisionScaleRoundedOperator monetaryOperator = PrecisionScaleRoundedOperator.of(scale, mathContext);

	MonetaryAmount result = monetaryOperator.apply(money);
	assertTrue(RoundedMoney.class.isInstance(result));
	assertEquals(result.getCurrency(), real);
	assertEquals(result.getNumber().getScale(), scale);
	assertEquals(result.getNumber().getPrecision(), precision);
	assertEquals(BigDecimal.valueOf(35.346), result.getNumber().numberValue(BigDecimal.class));



}
 
Example #5
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetExchangeRateProvider_Chained()
		throws InterruptedException {
	ExchangeRateProvider prov = MonetaryConversions
			.getExchangeRateProvider("ECB", "IMF");
	assertTrue(Objects.nonNull(prov));
	assertEquals(CompoundRateProvider.class, prov.getClass());
	// Test rate provided by IMF (derived)
	Thread.sleep(5000L); // wait for provider to load...
	ExchangeRate r = prov.getExchangeRate(Monetary.getCurrency("USD"),
			Monetary.getCurrency("INR"));
	assertTrue(Objects.nonNull(r));
	assertTrue(r.isDerived());
	// Test rate provided by ECB
	r = prov.getExchangeRate(Monetary.getCurrency("EUR"),
			Monetary.getCurrency("CHF"));
	assertTrue(Objects.nonNull(r));
	assertFalse(r.isDerived());
}
 
Example #6
Source File: ExchangeRateBuilderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void testNumberInsignificanceForRates(){
	ExchangeRate rateFromString = new ExchangeRateBuilder(ConversionContext.HISTORIC_CONVERSION)
			.setBase(Monetary.getCurrency("USD"))
			.setTerm(Monetary.getCurrency("EUR"))
			.setFactor(DefaultNumberValue.of(new BigDecimal("1.1")))
			.build();

	ExchangeRate rateFromDouble = new ExchangeRateBuilder(ConversionContext.HISTORIC_CONVERSION)
			.setBase(Monetary.getCurrency("USD"))
			.setTerm(Monetary.getCurrency("EUR"))
			.setFactor(DefaultNumberValue.of(1.1))
			.build();

	assertEquals(rateFromDouble, rateFromString, "Rates are not equal for same factor.");
}
 
Example #7
Source File: SmokeTest.java    From javamoney-lib with Apache License 2.0 6 votes vote down vote up
@Test
public void testExchange() {
    ExchangeRateProvider prov = MonetaryConversions.getExchangeRateProvider("ECB");
    assertNotNull(prov);
    ExchangeRate rate1 =
            prov.getExchangeRate(Monetary.getCurrency("CHF"), Monetary.getCurrency("EUR"));
    ExchangeRate rate2 =
            prov.getExchangeRate(Monetary.getCurrency("EUR"), Monetary.getCurrency("CHF"));
    ExchangeRate rate3 =
            prov.getExchangeRate(Monetary.getCurrency("CHF"), Monetary.getCurrency("USD"));
    ExchangeRate rate4 =
            prov.getExchangeRate(Monetary.getCurrency("USD"), Monetary.getCurrency("CHF"));
    System.out.println(rate1);
    System.out.println(rate2);
    System.out.println(rate3);
    System.out.println(rate4);
}
 
Example #8
Source File: MonetaryConversionTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetExchangeRateProvider_Chained()
		throws InterruptedException {
	ExchangeRateProvider prov = MonetaryConversions
			.getExchangeRateProvider("IMF");
	assertTrue(Objects.nonNull(prov));
	assertEquals(IMFRateProvider.class, prov.getClass());
	// Test rate provided by IMF (derived)
	Thread.sleep(5000L); // wait for provider to load...
	ExchangeRate exchangeRate = prov.getExchangeRate(Monetary.getCurrency("USD"),
			Monetary.getCurrency("INR"));
	assertTrue(Objects.nonNull(exchangeRate));
	assertTrue(exchangeRate.isDerived());
	// Test rate provided by ECB
	exchangeRate = prov.getExchangeRate(Monetary.getCurrency("EUR"),
			Monetary.getCurrency("CHF"));
	assertTrue(Objects.nonNull(exchangeRate));
	assertTrue(exchangeRate.isDerived());
}
 
Example #9
Source File: MonetaryConversionTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetExchangeRateProvider_Chained()
		throws InterruptedException {
	ExchangeRateProvider prov = MonetaryConversions
			.getExchangeRateProvider("ECB");
	assertTrue(Objects.nonNull(prov));
	assertEquals(ECBCurrentRateProvider.class, prov.getClass());
	// Test rate provided by IMF (derived)
	Thread.sleep(5000L); // wait for provider to load...
	ExchangeRate r = prov.getExchangeRate(Monetary.getCurrency("USD"),
			Monetary.getCurrency("INR"));
	assertTrue(Objects.nonNull(r));
	assertTrue(r.isDerived());
	// Test rate provided by ECB
	r = prov.getExchangeRate(Monetary.getCurrency("EUR"),
			Monetary.getCurrency("CHF"));
	assertTrue(Objects.nonNull(r));
	assertFalse(r.isDerived());
}
 
Example #10
Source File: FastMoneyTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for {@link org.javamoney.moneta.FastMoney#with(javax.money.MonetaryOperator)} .
 */
@Test
public void testWithMonetaryOperator() {
    MonetaryOperator adj = amount -> FastMoney.of(-100, amount.getCurrency());
    FastMoney m = FastMoney.of(new BigDecimal("1.2345"), "XXX");
    FastMoney a = m.with(adj);
    assertNotNull(a);
    assertNotSame(m, a);
    assertEquals(m.getCurrency(), a.getCurrency());
    assertEquals(FastMoney.of(-100, m.getCurrency()), a);
    adj = amount -> amount.multiply(2).getFactory().setCurrency(Monetary.getCurrency("CHF")).create();
    a = m.with(adj);
    assertNotNull(a);
    assertNotSame(m, a);
    assertEquals(Monetary.getCurrency("CHF"), a.getCurrency());
    assertEquals(FastMoney.of(1.2345 * 2, a.getCurrency()), a);
}
 
Example #11
Source File: ECBRateReadingHandler.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Override
public void startElement(String uri, String localName, String qName,
                         Attributes attributes) throws SAXException {
    if ("Cube".equals(qName)) {
        if (Objects.nonNull(attributes.getValue("time"))) {

            this.localDate = LocalDate.parse(attributes.getValue("time")).atStartOfDay().toLocalDate();
        } else if (Objects.nonNull(attributes.getValue("currency"))) {
            // read data <Cube currency="USD" rate="1.3349"/>
            CurrencyUnit tgtCurrency = Monetary
                    .getCurrency(attributes.getValue("currency"));
            addRate(tgtCurrency, this.localDate, BigDecimal.valueOf(Double
                    .parseDouble(attributes.getValue("rate"))));
        }
    }
    super.startElement(uri, localName, qName, attributes);
}
 
Example #12
Source File: BasicOperations.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	CurrencyUnit dollar = Monetary.getCurrency(Locale.US);
       MonetaryAmount money = Money.of(120, dollar);
       MonetaryAmount money2 = Money.of(50, dollar);
       System.out.println(money.add(money2));
       System.out.println(money.subtract(money2));
       System.out.println(money.multiply(2));
       System.out.println(money.divide(2));
       System.out.println(money.isEqualTo(money2));
       System.out.println(money.isGreaterThan(money2));
       System.out.println(money.isGreaterThanOrEqualTo(money2));
       System.out.println(money.isGreaterThanOrEqualTo(money2));
       System.out.println(money.isLessThan(money2));
       System.out.println(money.isLessThanOrEqualTo(money2));
       System.out.println(money.isNegative());
       System.out.println(money.isNegativeOrZero());
}
 
Example #13
Source File: AmountsDoCalculations.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
/**
   * @param args
   */
  public static void main(String[] args) {
      var amount = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(234).create();
      ConsoleUtils.printDetails(amount);

      amount = Monetary.getAmountFactory(FastMoney.class).setCurrency("EUR").setNumber(234).create();
      ConsoleUtils.printDetails(amount);

      amount = Monetary.getAmountFactory(
                 MonetaryAmountFactoryQueryBuilder.of().setMaxScale(50).setPrecision(30).build())
                   .setCurrency("EUR").setNumber(234).create();
      ConsoleUtils.printDetails(amount);

var amt1 = Money.of(10.1234556123456789, "USD");
var amt2 = FastMoney.of(123456789, "USD");

var total = amt1.add(amt2).multiply(0.5)
              .remainder(1);
      ConsoleUtils.printDetails(total);
  }
 
Example #14
Source File: MonetarySorterOperations.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
public static List<MonetaryAmount> getDollars() {
	CurrencyUnit dollar = Monetary.getCurrency(Locale.US);
	List<MonetaryAmount> moneys = new ArrayList<>();
	moneys.add(Money.of(120, dollar));
	moneys.add(Money.of(50, dollar));
	moneys.add(Money.of(80, dollar));
	moneys.add(Money.of(90, dollar));
	moneys.add(Money.of(120, dollar));
	return moneys;
}
 
Example #15
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 #16
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 #17
Source File: FutureValueTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Method: of(Rate ratePerPeriod, int periods).
 *
 * @throws Exception the exception
 */
@Test
public void testOfAndApply() throws Exception {
    Money money = Money.of(100, "CHF");
    MonetaryOperator rounding = Monetary.getRounding(RoundingQueryBuilder.of().setScale(2).set(RoundingMode.HALF_EVEN)
            .build());
    assertEquals(Money.of(BigDecimal.valueOf(105.00), "CHF"), money.with(FutureValue .of(RateAndPeriods.of(0.05, 1))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(110.25), "CHF"), money.with(FutureValue.of(RateAndPeriods.of(0.05, 2))).with(rounding));
    assertEquals(Money.of(BigDecimal.valueOf(210.49), "CHF"), money.with(FutureValue.of(RateAndPeriods.of(0.07, 11))).with(rounding));
}
 
Example #18
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 #19
Source File: CurrenciesProgrammaticallyRegister.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    CurrencyUnit onTheFlyUnit = new CurrencyUnit() {

        private CurrencyContext context = CurrencyContextBuilder.of("GeeCon-onTheFly").build();

        @Override
        public int compareTo(CurrencyUnit o) {
            return this.getCurrencyCode().compareTo(o.getCurrencyCode());
        }

        @Override
        public String getCurrencyCode() {
            return "GeeCon-Special";
        }

        @Override
        public int getNumericCode() {
            return 800;
        }

        @Override
        public int getDefaultFractionDigits() {
            return 0;
        }

        @Override
        public CurrencyContext getContext() {
            return context;
        }
    };
    ConfigurableCurrencyUnitProvider.registerCurrencyUnit(onTheFlyUnit);
    ConsoleUtils.printDetails(Monetary.getCurrency("GeeCon-Special"));
}
 
Example #20
Source File: BalloonLoanPaymentTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Apply.
 *
 * @throws Exception the exception
 */
@Test
public void apply() throws Exception {
    BalloonLoanPayment ci = BalloonLoanPayment.of(
            RateAndPeriods.of(0.05,2), Money.of(5, "CHF")
    );
    assertEquals(ci.apply(Money.of(1,"CHF")).with(Monetary.getDefaultRounding()),Money.of(-1.9,"CHF"));
}
 
Example #21
Source File: AmountEntry.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
public AmountEntry(String title) {
	super("AmountEntry.fxml");
	amountTitle.setText(title);
	numberType.getItems().add("BigDecimal");
	numberType.getItems().add("Long");
       for(CurrencyUnit cu: Monetary.getCurrencies()){
           codeBox.getItems().add(cu.getCurrencyCode());
       }
       Collections.sort(codeBox.getItems());
       codeBox.getSelectionModel().select("CHF");
}
 
Example #22
Source File: BalloonLoanPaymentTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate two periods.
 *
 * @throws Exception the exception
 */
@Test
public void calculate_twoPeriods() throws Exception {
    BalloonLoanPayment ci = BalloonLoanPayment.of(
            RateAndPeriods.of(0.05,2), Money.of(5, "CHF")
    );
    assertEquals(Money.of(100,"CHF").with(ci).with(Monetary.getDefaultRounding()),Money.of(51.34,"CHF"));
    assertEquals(Money.of(0,"CHF").with(ci).with(Monetary.getDefaultRounding()),Money.of(-2.44,"CHF"));
    assertEquals(Money.of(-1,"CHF").with(ci).with(Monetary.getDefaultRounding()),Money.of(-2.98,"CHF"));
}
 
Example #23
Source File: BalloonLoanPaymentTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate one periods.
 *
 * @throws Exception the exception
 */
@Test
public void calculate_onePeriods() throws Exception {
    BalloonLoanPayment ci = BalloonLoanPayment.of(
            RateAndPeriods.of(0.05,1), Money.of(5, "CHF")
    );
    assertEquals(Money.of(100,"CHF").with(ci).with(Monetary.getDefaultRounding()),Money.of(100,"CHF"));
    assertEquals(Money.of(0,"CHF").with(ci).with(Monetary.getDefaultRounding()),Money.of(-5,"CHF"));
    assertEquals(Money.of(-1,"CHF").with(ci).with(Monetary.getDefaultRounding()),Money.of(-6.05,"CHF"));
}
 
Example #24
Source File: CurrenciesAccess.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
    ConsoleUtils.printDetails(Monetary.getCurrency("CHF"));

    Monetary.getCurrencies(CurrencyQueryBuilder.of().setProviderNames("ConfigurableCurrencyUnitProvider").build()).forEach(
            ConsoleUtils::printDetails
    );

    CurrencyUnitBuilder.of("Devoxx", "Devoxx-Conference").build(true);
    Monetary.getCurrencies(CurrencyQueryBuilder.of().setProviderNames("ConfigurableCurrencyUnitProvider").build()).forEach(
            ConsoleUtils::printDetails
    );
}
 
Example #25
Source File: ExtractorMajorPartOperatorTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnNegativeValue() {
	CurrencyUnit currency = Monetary.getCurrency("BHD");
	MonetaryAmount money = Money.parse("BHD -1.345");
	MonetaryAmount result = operator.apply(money);
	assertEquals(result.getCurrency(), currency);
	assertEquals(result.getNumber().doubleValue(), -1.0);

}
 
Example #26
Source File: SmokeTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void testAmountFormatRoundTrip() throws ParseException {
    // Using parsers
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(Locale.GERMANY);
    assertNotNull(format);
    MonetaryAmount amount = Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(10.50).create();
    String formatted = format.format(amount);
    assertNotNull(formatted);
    MonetaryAmount parsed = format.parse(formatted);
    assertNotNull(parsed);
    assertEquals(amount, parsed);
}
 
Example #27
Source File: SmokeTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateAmounts() {
    // Creating one
    CurrencyUnit currency = Monetary.getCurrency("CHF");
    Money amount1 = Money.of(1.0d, currency);
    Money amount2 = Money.of(1.0d, currency);
    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 #28
Source File: MonetaryAmountFormatTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for {@link javax.money.format.MonetaryAmountFormat#parse(java.lang.CharSequence)}
 */
@Test
public void testParse() throws ParseException {
    MonetaryAmountFormat defaultFormat = MonetaryFormats.getAmountFormat(GERMANY);
    MonetaryAmount amountEur = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(new BigDecimal("12.50")).create();
    MonetaryAmount amountChf = Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("12.50")).create();
    assertEquals(amountEur, defaultFormat.parse("12,50 EUR"));
    assertEquals(amountEur, defaultFormat.parse("\u00A0 \u202F \u2007 12,50 EUR"));
    assertEquals(amountEur, defaultFormat.parse("\u00A0 \u202F \u2007 12,50 \u00A0 \u202F \u2007 EUR  \t\n\n "));
    assertEquals(amountChf, defaultFormat.parse("12,50 CHF"));
}
 
Example #29
Source File: CurrenciesTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link javax.money.Monetary#getCurrency(java.lang.String, String...)}.
 * .
 */
@Test
public void testGetMultipleInstancesString() {
	CurrencyUnit cur = Monetary.getCurrency("USD");
	CurrencyUnit cur2 = Monetary.getCurrency("USD");
	assertNotNull(cur2);
	assertSame(cur, cur2);
	Currency jdkCurrency = Currency.getInstance("USD");
	assertEquals(jdkCurrency.getCurrencyCode(), cur.getCurrencyCode());
	assertEquals(jdkCurrency.getNumericCode(), cur.getNumericCode());
	assertEquals(jdkCurrency.getDefaultFractionDigits(),
			cur.getDefaultFractionDigits());
}
 
Example #30
Source File: PercentOperatorTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnPositiveValue() {
	CurrencyUnit currency = Monetary.getCurrency("EUR");
	MonetaryAmount money = Money.parse("EUR 200.0");
	MonetaryAmount result = operator.apply(money);
	assertEquals(result.getCurrency(), currency);
	assertEquals(result.getNumber().doubleValue(), 20.0);

}