如何使用货币符号格式化MonetaryAmount?

时间:2017-02-07 19:19:53

标签: java jsr354

编辑:此问题旨在用于JSR-354上下文中的格式设置。另一种方法是使用java.util.CurrencyFormat。但我想知道如何使用MonetaryAmountFormat

如何在JSR-354 MonetaryAmount的上下文中使用货币符号格式化MonetaryAmountFormat

我尝试使用MonetaryAmountFormat,如下所示,我在网上找到了两个不同的例子。这两种方法都没有正常工作,所以我将我的示例测试用例推送到github,以防您希望克隆并查看完整的详细信息,但这是测试:

的pom.xml :   

<dependency>
  <groupId>org.javamoney</groupId>
  <artifactId>moneta</artifactId>
  <version>1.1</version>
</dependency>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>

TestMonetaryAmountFormat.java

package com.github.axiopisty.stackoverflow.jsr354;

import java.math.BigDecimal;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import javax.money.CurrencyUnit;
import javax.money.Monetary;
import javax.money.MonetaryAmount;
import javax.money.format.AmountFormatQueryBuilder;
import javax.money.format.MonetaryAmountFormat;
import javax.money.format.MonetaryFormats;
import org.javamoney.moneta.Money;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class TestMonetaryAmountFormat {

  private final String EXPECTED = "$12.99";

  private final CurrencyUnit USD = Monetary.getCurrency("USD");
  private final MonetaryAmount AMOUNT = Money.of(new BigDecimal("12.99"), USD);

  /**
   * See: http://www.programcreek.com/java-api-examples/index.php?api=javax.money.format.MonetaryAmountFormat
   */
  @Test
  public void testFormatWithCurrencySymbol_1() {
    MonetaryAmountFormat maf = MonetaryFormats.getAmountFormat(
      AmountFormatQueryBuilder
        .of(Locale.US)
        .setFormatName("SYMBOL")
        .build()
    );
    String actual = maf.format(AMOUNT);
    assertEquals(EXPECTED, actual);
    /**
     * The previous assert statement results in the following:
     *
     * javax.money.MonetaryException: No MonetaryAmountFormat for AmountFormatQuery AmountFormatQuery (
     * {Query.formatName=SYMBOL, java.util.Locale=en_US})
     * at javax.money.spi.MonetaryFormatsSingletonSpi.getAmountFormat(MonetaryFormatsSingletonSpi.java:71)
     * at javax.money.format.MonetaryFormats.getAmountFormat(MonetaryFormats.java:110)
     * at com.github.axiopisty.stackoverflow.jsr354.TestMonetaryAmountFormat.testFormatWithCurrencySymbol_1(TestMonetaryAmountFormat.java:26)
     * ...
     **/
  }

  /**
   * See: https://github.com/JavaMoney/javamoney-examples/issues/25
   */
  @Test
  public void testFormatWithCurrencySymbol_2() {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    MonetaryAmountFormat maf = MonetaryFormats.getAmountFormat(
      AmountFormatQueryBuilder
        .of(Locale.US)
        .set(symbols)
        .build()
    );
    String actual = maf.format(AMOUNT);

    assertEquals(EXPECTED, actual);
    /**
     * The previous assert statement results in the following:
     *
     * org.junit.ComparisonFailure:
     * Expected :$12.99
     * Actual   :USD12.99
     */
  }

}

1 个答案:

答案 0 :(得分:5)

在提出问题后我才找到了答案。我更新了github项目以包含解决方案。

  /**
   * See http://stackoverflow.com/q/40244526/328275
   */
  @Test
  public void test3() {
    final MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(
      AmountFormatQueryBuilder.of(Locale.US)
        .set(CurrencyStyle.SYMBOL)
        .build()
    );
    final String actual = format.format(AMOUNT);
    assertEquals(EXPECTED, actual);
  }