如何使用货币符号格式化货币金额



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

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

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

绒球.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>

测试货币金额格式.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
     */
  }
}

我在问完这个问题后就找到了答案。我更新了 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);
  }

最新更新