Search in sources :

Example 1 with MonetaryAmountFormat

use of javax.money.format.MonetaryAmountFormat in project tutorials by eugenp.

the class JavaMoneyUnitManualTest method givenAmount_whenCustomFormat_thanEquals.

@Test
public void givenAmount_whenCustomFormat_thanEquals() {
    MonetaryAmount oneDollar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create();
    MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.NAME).set("pattern", "00000.00 �").build());
    String customFormatted = customFormat.format(oneDollar);
    assertNotNull(customFormat);
    assertEquals("USD 1", oneDollar.toString());
    assertEquals("00001.00 US Dollar", customFormatted);
}
Also used : MonetaryAmount(javax.money.MonetaryAmount) MonetaryAmountFormat(javax.money.format.MonetaryAmountFormat) Test(org.junit.Test)

Example 2 with MonetaryAmountFormat

use of javax.money.format.MonetaryAmountFormat in project jsr354-ri by JavaMoney.

the class MonetaryAmountFormatTest method testFormat.

/**
 * Test method for
 * {@link javax.money.format.MonetaryAmountFormat#format(javax.money.MonetaryAmount)} .
 */
@Test
public void testFormat() {
    MonetaryAmountFormat defaultFormat = MonetaryFormats.getAmountFormat(Locale.GERMANY);
    assertEquals("12,50 CHF", defaultFormat.format(Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(12.50).create()));
    assertEquals("123.456.789.101.112,12 INR", defaultFormat.format(Monetary.getDefaultAmountFactory().setCurrency("INR").setNumber(123456789101112.123456).create()));
    defaultFormat = MonetaryFormats.getAmountFormat(new Locale("", "IN"));
    assertEquals("CHF 1,211,112.50", defaultFormat.format(Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(1211112.50).create()));
    assertEquals("INR 123,456,789,101,112.12", defaultFormat.format(Monetary.getDefaultAmountFactory().setCurrency("INR").setNumber(123456789101112.123456).create()));
// Locale india = new Locale("", "IN");
// defaultFormat = MonetaryFormats.getAmountFormatBuilder(india)
// .setNumberGroupSizes(3, 2).of();
// assertEquals("INR 12,34,56,78,91,01,112.12",
// defaultFormat.format(Monetary.getAmount("INR",
// 123456789101112.123456)));
}
Also used : Locale(java.util.Locale) MonetaryAmountFormat(javax.money.format.MonetaryAmountFormat) Test(org.testng.annotations.Test)

Example 3 with MonetaryAmountFormat

use of javax.money.format.MonetaryAmountFormat in project jsr354-ri by JavaMoney.

the class MonetaryAmountFormatTest method testFormatWithBuilder2.

/**
 * Test method for
 * {@link javax.money.format.MonetaryAmountFormat#format(javax.money.MonetaryAmount)} .
 */
@Test
public void testFormatWithBuilder2() {
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.GERMANY).set(CurrencyStyle.NUMERIC_CODE).build());
    assertEquals("12,50 756", format.format(Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(12.50).create()));
    format = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.SYMBOL).build());
    assertEquals("$123,456.56", format.format(Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(123456.561).create()));
}
Also used : MonetaryAmountFormat(javax.money.format.MonetaryAmountFormat) Test(org.testng.annotations.Test)

Example 4 with MonetaryAmountFormat

use of javax.money.format.MonetaryAmountFormat in project jsr354-ri by JavaMoney.

the class MonetaryAmountFormatTest method testParse.

/**
 * Test method for {@link javax.money.format.MonetaryAmountFormat#parse(java.lang.CharSequence)}
 * .
 *
 * @throws ParseException
 */
@Test
public void testParse() throws ParseException {
    MonetaryAmountFormat defaultFormat = MonetaryFormats.getAmountFormat(Locale.GERMANY);
    assertEquals(Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(new BigDecimal("12.50")).create(), defaultFormat.parse("12,50 EUR"));
    assertEquals(Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(new BigDecimal("12.50")).create(), defaultFormat.parse("  \t 12,50 EUR"));
    assertEquals(Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(new BigDecimal("12.50")).create(), defaultFormat.parse("  \t 12,50 \t\n EUR  \t\n\n "));
    assertEquals(Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("12.50")).create(), defaultFormat.parse("12,50 CHF"));
}
Also used : MonetaryAmountFormat(javax.money.format.MonetaryAmountFormat) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 5 with MonetaryAmountFormat

use of javax.money.format.MonetaryAmountFormat in project jsr354-ri by JavaMoney.

the class MonetaryAmountFormatTest method testFormatWithBuilder.

/**
 * Test method for
 * {@link javax.money.format.MonetaryAmountFormat#format(javax.money.MonetaryAmount)} .
 */
@Test
public void testFormatWithBuilder() {
    MonetaryAmountFormat defaultFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.JAPANESE).build());
    assertEquals("CHF 12.50", defaultFormat.format(Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(12.50).create()));
}
Also used : MonetaryAmountFormat(javax.money.format.MonetaryAmountFormat) Test(org.testng.annotations.Test)

Aggregations

MonetaryAmountFormat (javax.money.format.MonetaryAmountFormat)10 Test (org.testng.annotations.Test)8 Locale (java.util.Locale)4 MonetaryAmount (javax.money.MonetaryAmount)4 Test (org.junit.Test)2 BigDecimal (java.math.BigDecimal)1 Money (org.javamoney.moneta.Money)1 BeforeTest (org.testng.annotations.BeforeTest)1