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

How to format MonetaryAmount with currency symbol?

编辑: 这个问题是为了在 JSR-354 的上下文中进行格式化。另一种方法是使用 java.util.CurrencyFormat。但我想知道如何使用 MonetaryAmountFormat.

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

我尝试使用 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.Whosebug.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 = ".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.Whosebug.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 :.99
     * Actual   :USD12.99
     */
  }

}

刚问完问题就找到了答案。我更新了 github 项目以包含该解决方案。

  /**
   * See 
   */
  @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);
  }