JavaMoney / jsr354-ri

JSR 354 - Moneta: Reference Implementation
Other
344 stars 101 forks source link

¤¤ #296

Closed stokito closed 4 years ago

stokito commented 5 years ago

The JavaMoney's formatter internally reuses a DecimalFormat from JDK and uses the same patterns. For example the pattern 00.0 ¤ says that currency code or symbol is on the end of amount. Where it is a currency code or a symbol is determined by set(CurrencyStyle.CODE) in format context. If the CurrencyStyle wasn't set the it will be used the CODE by default.

But in the JavaDoc of the DecimalFormat said:

¤ (\u00A4) Currency sign, replaced by currency symbol. If doubled, replaced by international currency symbol. If present in a pattern, the monetary decimal separator is used instead of the decimal separator.

Which means that we can specify when to use a code or a symbol by the pattern itself. Here is a test case to demonstrate:

    @Test
    public void testNameCurrencySymbol() {
        DecimalFormat format = (DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.US);
        format.applyPattern("0.00 ¤");
        format.setCurrency(Currency.getInstance("USD"));`
        String formatted = format.format(42);
        Assert.assertEquals(formatted, "42.00 $");
        format.applyPattern("0.00 ¤¤");
        formatted = format.format(42);
        Assert.assertEquals(formatted, "42.00 USD");
    }

So actually users who are familiar with the pattern ¤¤ may be confused because it doesn't supported by JavaMoney and fails at all. What is interesting is that in CLDR Number Patterns this is not mentioned.

So what we can do:

  1. Ignore the ¤¤ and fail as it's now. At least it wasn't mentioned in CLDR patterns.
  2. Do not fail if pattern contains two currency symbols - we can just treat it as one currency sign and use a symbol or code only depending on CurrencyStyle.
  3. Be complaint with JDK and if not specified CurrencyStyle then use a symbol for one ¤ and currency code for ¤¤ but this will be a breaking change.
keilw commented 5 years ago

How complex is it to fix this in the MR? We should not suffer from Scope Creep especially because EVERY FIX here MUST be back-ported to Moneta-BP, while the next JSR should have a universal RI with Java 9 Multi-Release JARs where necessary to be compatible with Java 8 or similar.

atsticks commented 4 years ago

The test case works with my latest changes. Not sure if it really solves the problem.