Java源码示例:javax.money.format.MonetaryFormats

示例1
@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 US Dollar")
      .build());
    String customFormatted = customFormat.format(oneDollar);

    assertNotNull(customFormat);
    assertEquals("USD 1", oneDollar.toString());
    assertEquals("00001.00 US Dollar", customFormatted);
}
 
示例2
/**
 * Test method for
 * {@link javax.money.format.MonetaryAmountFormat#format(javax.money.MonetaryAmount)} .
 */
@Test
public void testFormat() {
    MonetaryAmountFormat defaultFormat = MonetaryFormats.getAmountFormat(GERMANY);
    MonetaryAmount amountChf1 = Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(12.50).create();
    assertEquals("12,50 CHF", defaultFormat.format(amountChf1));
    MonetaryAmount amountInr = Monetary.getDefaultAmountFactory().setCurrency("INR").setNumber(123456789101112.123456).create();
    assertEquals("123.456.789.101.112,12 INR", defaultFormat.format(amountInr));
    defaultFormat = MonetaryFormats.getAmountFormat(new Locale("", "IN"));
    MonetaryAmount amountChf = Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(1211112.50).create();
    assertEquals("CHF 12,11,112.50", defaultFormat.format(amountChf));
    assertEquals("INR 12,34,56,78,91,01,112.12", defaultFormat.format(amountInr));
    // 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)));
}
 
示例3
/**
 * Test related to https://github.com/JavaMoney/jsr354-ri/issues/294
 */
@Test
public void testParse_amount_without_currency_code_but_with_currency_in_context() {
    CurrencyUnit eur = Monetary.getCurrency("EUR");
    AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(GERMANY)
        .set(CurrencyUnit.class, eur)
        .build();
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery);
    try {
        MonetaryAmount parsedAmount = format.parse("0.01");
        assertSame(parsedAmount.getCurrency(), eur);
        assertEquals(parsedAmount.getNumber().doubleValueExact(), 0.01D);
        assertEquals(parsedAmount.toString(), "EUR 0.01");
    } catch (MonetaryParseException e) {
        assertEquals(e.getMessage(), "Error parsing CurrencyUnit: no input.");
        assertEquals(e.getErrorIndex(), -1);
    }
}
 
示例4
/**
 * Test related to parsing currency symbols.
 */
@Test
//"see https://github.com/JavaMoney/jsr354-ri/issues/274"
public void testParseCurrencySymbolINR1() {
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(
                AmountFormatQueryBuilder.of(Locale.GERMANY)
                    .set(CurrencyStyle.SYMBOL)
                    .build());
    Money money = Money.of(new BigDecimal("1234567.89"), "EUR");
    String expectedFormattedString = "1.234.567,89 €";
    assertEquals(expectedFormattedString, format.format(money));
    assertEquals(money, Money.parse(expectedFormattedString, format));

    money = Money.of(new BigDecimal("1234567.89"), "INR");
    expectedFormattedString = "1.234.567,89 ₹";
    assertEquals(expectedFormattedString, format.format(money));
    assertEquals(money, Money.parse(expectedFormattedString, format));
}
 
示例5
/**
 * Test related to parsing currency symbols.
 */
@Test
//"see https://github.com/JavaMoney/jsr354-ri/issues/274"
public void testParseCurrencySymbolINR2() {
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(
                AmountFormatQueryBuilder.of(INDIA)
                    .set(CurrencyStyle.SYMBOL)
                    .build());
    Money money = Money.of(new BigDecimal("1234567.89"), "EUR");
    String expectedFormattedString = "€ 1,234,567.89";
    assertEquals(expectedFormattedString, format.format(money));
    assertEquals(money, Money.parse(expectedFormattedString, format));

    money = Money.of(new BigDecimal("1234567.89"), "INR");
    expectedFormattedString = "₹ 1,234,567.89";
    assertEquals(expectedFormattedString, format.format(money));
    assertEquals(money, Money.parse(expectedFormattedString, format));
}
 
示例6
public static void main(String... args) {
    MonetaryAmount amt = Money.of(1234.5678, "EUR");
    System.out.println(amt.query(MonetaryFormats.getAmountFormat(Locale.GERMANY)));
    System.out.println(MonetaryFormats.getAmountFormat(Locale.GERMANY).format(amt));
    amt = Money.of(123412341234.5678, "INR");
    System.out.println(MonetaryFormats.getAmountFormat(new Locale("", "INR")).format(amt));

    // no with adaptive groupings
    System.out.println(MonetaryFormats.getAmountFormat(
            AmountFormatQueryBuilder.of(new Locale("", "INR"))
                    .set(AmountFormatParams.GROUPING_SIZES, new int[]{2, 3})
                    .set(AmountFormatParams.GROUPING_GROUPING_SEPARATORS, new char[]{',', '`'})
                    .build())
            .format(amt));
    
    amt = Money.of(5, "USD");
    System.out.println(MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.SYMBOL).set(AmountFormatParams.PATTERN, "¤##.##").build()).format(amt));
}
 
示例7
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());
}
 
示例8
public static void main(String... args) {
	var amt = Money.of(1234.5678, "EUR");
	System.out.println(amt.query(MonetaryFormats.getAmountFormat(Locale.GERMANY)));
	System.out.println(MonetaryFormats.getAmountFormat(Locale.GERMANY).format(amt));
	amt = Money.of(123412341234.5678, "INR");
	System.out.println(MonetaryFormats.getAmountFormat(new Locale("", "INR")).format(amt));

	// no with adaptive groupings
	System.out.println(MonetaryFormats
			.getAmountFormat(AmountFormatQueryBuilder.of(new Locale("", "INR"))
					.set(AmountFormatParams.GROUPING_SIZES, new int[] { 2, 3 })
					.set(AmountFormatParams.GROUPING_GROUPING_SEPARATORS, new char[] { ',', '`' }).build())
			.format(amt));

	amt = Money.of(5, "USD");
	System.out.println(MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US)
			.set(CurrencyStyle.SYMBOL).set(AmountFormatParams.PATTERN, "¤##.##").build()).format(amt));
}
 
示例9
public static void main(String... args) {
    MonetaryAmount amt = Money.of(1234.5678, "EUR");
    System.out.println(amt.query(MonetaryFormats.getAmountFormat(Locale.GERMANY)));
    System.out.println(MonetaryFormats.getAmountFormat(Locale.GERMANY).format(amt));
    amt = Money.of(123412341234.5678, "INR");
    System.out.println(MonetaryFormats.getAmountFormat(new Locale("", "INR")).format(amt));

    // now with adaptive groupings
    System.out.println(MonetaryFormats.getAmountFormat(
            AmountFormatQueryBuilder.of(new Locale("", "INR"))
                    .set(AmountFormatParams.GROUPING_SIZES, new int[]{3, 2})
                    .set(AmountFormatParams.GROUPING_GROUPING_SEPARATORS, new char[]{',', '`'})
                    .build())
            .format(amt));
    
    amt = Money.of(5, "USD");
    System.out.println(MonetaryFormats.getAmountFormat(
            AmountFormatQueryBuilder.of(Locale.US)
                .set(CurrencyStyle.SYMBOL)
                .set(AmountFormatParams.PATTERN, "¤##.##")
                .build())
            .format(amt));
}
 
示例10
private void applyJavaMoneyHack() {
    //fixes https://github.com/commercetools/commercetools-sunrise-java/issues/404
    //exception play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.IllegalArgumentException: java.util.concurrent.CompletionException: io.sphere.sdk.json.JsonException: detailMessage: com.fasterxml.jackson.databind.JsonMappingException: Operator failed: javax.mon[email protected]1655879e (through reference chain: io.sphere.sdk.payments.PaymentDraftImpl["amountPlanned"])
    Monetary.getDefaultRounding();
    Monetary.getDefaultRounding().apply(MoneyImpl.ofCents(123, "EUR"));
    Monetary.getDefaultAmountType();
    MonetaryFormats.getDefaultFormatProviderChain();
    Monetary.getDefaultCurrencyProviderChain();
}
 
示例11
@Override
public String format(final MonetaryAmount monetaryAmount) {
    final boolean isDecimal = monetaryAmount.getNumber().doubleValueExact() % 1 != 0;//TODO this can be improved with monetary query
    final AmountFormatQuery pattern = AmountFormatQueryBuilder.of(locale)
            .set(CurrencyStyle.SYMBOL)
            .set(PATTERN, isDecimal ? PATTERN_WITH_DECIMAL : PATTERN_WITHOUT_DECIMAL)
            .build();
    return MonetaryFormats.getAmountFormat(pattern).format(monetaryAmount);
}
 
示例12
@Test
public void givenLocale_whenFormatted_thanEquals() {
    MonetaryAmount oneDollar = Monetary
      .getDefaultAmountFactory()
      .setCurrency("USD")
      .setNumber(1)
      .create();
    MonetaryAmountFormat formatUSD = MonetaryFormats.getAmountFormat(Locale.US);
    String usFormatted = formatUSD.format(oneDollar);

    assertEquals("USD 1", oneDollar.toString());
    assertNotNull(formatUSD);
    assertEquals("USD1.00", usFormatted);
}
 
示例13
@Produces @Dependent
public static MonetaryAmountFormat amountFormat(InjectionPoint ip){
    AmountFormat specAnnot = ip.getAnnotated()!=null?ip.getAnnotated().getAnnotation(AmountFormat.class):null;
    if(specAnnot==null){
        throw new IllegalArgumentException("@FormatName is required.");
    }
    return MonetaryFormats.getAmountFormat(createAmountFormatQuery(
            specAnnot,
            ip.getAnnotated().getAnnotation(Amount.class)));
}
 
示例14
@Produces @Dependent
public static Collection<MonetaryAmountFormat> amountFormats(InjectionPoint ip){
    AmountFormat specAnnot = ip.getAnnotated()!=null?ip.getAnnotated().getAnnotation(AmountFormat.class):null;
    if(specAnnot==null){
        throw new IllegalArgumentException("@FormatName is required.");
    }
    return MonetaryFormats.getAmountFormats(createAmountFormatQuery(
            specAnnot,
            ip.getAnnotated().getAnnotation(Amount.class)));
}
 
示例15
@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);
}
 
示例16
@Test
public void testCashRoundings(){
    CurrencyUnit chf = CurrencyUnitBuilder.of("CHF","").build();
    CurrencyUnit eur = CurrencyUnitBuilder.of("EUR","").build();
    CurrencyUnit sek = CurrencyUnitBuilder.of("SEK","").build();

    MonetaryRounding rounding = Monetary.getRounding(
            RoundingQueryBuilder.of()
                    .setCurrency(chf)
                    .set("cashRounding", true).build());

    MonetaryRounding roundingEUR = Monetary.getRounding(
            RoundingQueryBuilder.of()
                    .setCurrency(eur)
                    .set("cashRounding", true).build());

    MonetaryRounding roundingSEK = Monetary.getRounding(
            RoundingQueryBuilder.of()
                    .setCurrency(sek)
                    .set("cashRounding", true).build());

    System.out.println(MonetaryFormats.getAmountFormat(new Locale("ch", "")).format(Money.of(3459.97,chf)));
    System.out.println(MonetaryFormats.getAmountFormat(new Locale("ch", "")).format(Money.of(3459.97,chf).with(rounding)));

    System.out.println(MonetaryFormats.getAmountFormat(new Locale("de", "")).format(Money.of(3459.97,eur)));
    System.out.println(MonetaryFormats.getAmountFormat(new Locale("de", "")).format(Money.of(3459.97,eur).with(roundingEUR)));

    System.out.println(MonetaryFormats.getAmountFormat(new Locale("sv", "")).format(Money.of(3459.97,sek)));
    System.out.println(MonetaryFormats.getAmountFormat(new Locale("sv", "")).format(Money.of(3459.97,sek).with(roundingSEK)));

}
 
示例17
/**
 * Test method for
 * {@link javax.money.format.MonetaryAmountFormat#format(javax.money.MonetaryAmount)} .
 */
@Test
public void testFormatWithBuilder() {
    AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(JAPANESE).build();
    MonetaryAmountFormat defaultFormat = MonetaryFormats.getAmountFormat(formatQuery);
    MonetaryAmount amountChf = Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(12.50).create();
    assertEquals("CHF12.50", defaultFormat.format(amountChf));
}
 
示例18
/**
 * Test method for
 * {@link javax.money.format.MonetaryAmountFormat#format(javax.money.MonetaryAmount)} .
 */
@Test
public void testFormatWithBuilder2() {
    AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(GERMANY).set(NUMERIC_CODE).build();
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery);
    MonetaryAmount amountChf = Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(12.50).create();
    assertEquals("12,50 756", format.format(amountChf));
    format = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(US).set(SYMBOL).build());
    MonetaryAmount amountUsd = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(123456.561).create();
    assertEquals("$123,456.56", format.format(amountUsd));
}
 
示例19
/**
 * Test method for
 * {@link javax.money.format.MonetaryAmountFormat#print(java.lang.Appendable, javax.money.MonetaryAmount)}
 */
@Test
public void testPrint() throws IOException {
    StringBuilder b = new StringBuilder();
    MonetaryAmountFormat defaultFormat = MonetaryFormats.getAmountFormat(GERMANY);
    defaultFormat.print(b, Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(12.50).create());
    assertEquals("12,50 CHF", b.toString());
    b.setLength(0);
    defaultFormat.print(b, Monetary.getDefaultAmountFactory().setCurrency("INR")
            .setNumber(123456789101112.123456).create());
    assertEquals("123.456.789.101.112,12 INR", b.toString());
    b.setLength(0);
    defaultFormat = MonetaryFormats.getAmountFormat(new Locale("", "IN"));
    defaultFormat
            .print(b, Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(1211112.50).create());
    assertEquals("CHF 12,11,112.50", b.toString());
    b.setLength(0);
    defaultFormat.print(b, Monetary.getDefaultAmountFactory().setCurrency("INR")
            .setNumber(123456789101112.123456).create());
    assertEquals("INR 12,34,56,78,91,01,112.12", b.toString());
    b.setLength(0);
    // Locale india = new Locale("", "IN");
    // defaultFormat = MonetaryFormats.getAmountFormat(india)
    // .setNumberGroupSizes(3, 2).of();
    // defaultFormat.print(b, Monetary.getAmount("INR",
    // 123456789101112.123456));
    // assertEquals("INR 12,34,56,78,91,01,112.12",
    // b.toString());
}
 
示例20
/**
 * 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"));
}
 
示例21
@Test
public void testBulgarianLev(){
    MonetaryAmount money = Money.of(1123000.50, "BGN");
    Locale locale = new Locale("bg", "BG");
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(
            AmountFormatQueryBuilder.of(locale).set(SYMBOL)
                    .build());
    assertEquals(format.format(money), "1 123 000,50 лв.");

    format = MonetaryFormats.getAmountFormat(
            AmountFormatQueryBuilder.of(locale).set(CODE)
                    .build());
    assertEquals(format.format(money), "1 123 000,50 BGN");
}
 
示例22
@Test
public void testParse_DKK_da() {
    AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(DANISH).set(CODE).build();
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery);
    assertMoneyParse(format, "00000123 DKK", 123.0, "DKK");
    assertMoneyParse(format, "123 DKK", 123.0, "DKK");
    assertMoneyParse(format, "123,01 DKK", 123.01, "DKK");
    assertMoneyParse(format, "14.000,12 DKK", 14000.12, "DKK");
    assertMoneyParse(format, "14.000,12\u00A0DKK", 14000.12, "DKK");
}
 
示例23
@Test
public void testFormat_DKK_da() {
    AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(DANISH).set(CODE).build();
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery);
    assertMoneyFormat(format,  Money.of(123.01, "DKK"), "123,01 DKK");
    assertMoneyFormat(format, Money.of(14000.12, "DKK"), "14.000,12 DKK");
}
 
示例24
@Test
public void testParse_EUR_fr_FR() {
    AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(FRANCE).set(CODE).build();
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery);
    assertMoneyParse(format, "00000123 EUR", 123.0, "EUR");
    assertMoneyParse(format, "123 EUR", 123.0, "EUR");
    assertMoneyParse(format, "123,01 EUR", 123.01, "EUR");
    assertMoneyParse(format, "14 000,12 EUR", 14000.12, "EUR");
    assertMoneyParse(format, "14\u00A0000,12\u00A0EUR", 14000.12, "EUR");
    assertMoneyParse(format, "14\u202F000,12\u00A0EUR", 14000.12, "EUR");
}
 
示例25
@Test
public void testFormat_EUR_fr_FR() {
    AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(FRANCE).set(CODE).build();
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery);
    assertMoneyFormat(format,  Money.of(123.01, "EUR"), "123,01 EUR");
    assertMoneyFormat(format,  Money.of(14000.12, "EUR"), "14 000,12 EUR");
}
 
示例26
@Test
public void testParse_BGN_bg_BG() {
    AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(BULGARIA).set(CODE).build();
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery);
    assertMoneyParse(format, "00000123 BGN", 123.0, "BGN");
    assertMoneyParse(format, "123 BGN", 123.0, "BGN");
    assertMoneyParse(format, "123,01 BGN", 123.01, "BGN");
    assertMoneyParse(format, "14000,12 BGN", 14000.12, "BGN");
    assertMoneyParse(format, "14\u00A0000,12 BGN", 14000.12, "BGN");
    assertMoneyParse(format, "14 000,12 BGN", 14000.12, "BGN");
}
 
示例27
@Test
public void testFormat_BGN_bg_BG() {
    AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(BULGARIA).set(CODE).build();
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery);
    assertMoneyFormat(format,  Money.of(123.01, "BGN"), "123,01 BGN");
    assertMoneyFormat(format, Money.of(14000.12, "BGN"), "14 000,12 BGN");
}
 
示例28
@Test
public void testParse_EUR_de_DE() {
    AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(GERMANY).set(CODE).build();
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery);
    assertMoneyParse(format, "00000123 EUR", 123.0, "EUR");
    assertMoneyParse(format, "123 EUR", 123.0, "EUR");
    assertMoneyParse(format, "123,01 EUR", 123.01, "EUR");
    assertMoneyParse(format, "14.000,12 EUR", 14000.12, "EUR");
    assertMoneyParse(format, "14.000,12\u00A0EUR", 14000.12, "EUR");
}
 
示例29
@Test
public void testFormat_EUR_de_DE() {
    AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(GERMANY).set(CODE).build();
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery);
    assertMoneyFormat(format, Money.of(123.01, "EUR"), "123,01 EUR");
    assertMoneyFormat(format, Money.of(14000.12, "EUR"), "14.000,12 EUR");
}
 
示例30
@Test
public void testParse_CNY_zh_CN() {
    AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(CHINA).set(CODE).build();
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery);
    assertMoneyParse(format, "CNY00000123", 123.0, "CNY");
    assertMoneyParse(format, "CNY123.01", 123.01, "CNY");
    assertMoneyParse(format, "CNY14,000.12", 14000.12, "CNY");
    assertMoneyParse(format, "CNY 14,000.12", 14000.12, "CNY");
    assertMoneyParse(format, "CNY\u00A014,000.12", 14000.12, "CNY");
}