3

First of all, like the title says, this is about android.icu.text.DecimalFormat, and not java.text.DecimalFormat.

I'm using DecimalFormat to get decimalFormatSymbols.monetaryGroupingSeparator and decimalFormatSymbols.monetaryDecimalSeparator, and then use them to format amount input from the user.

DecimalFormat also has a withCurrency input, which from I see, shouldn't affect the monetary separators.

But, for some locales (en_SE and en_DK) and currencies (EUR), it does change the monetary separators. As you can see, these are english locales for countries in EU which do not have EUR as currency.

From what I see we have the following cases:

English EU EUR locale set EUR set GBP
X X X en_AT,BE,DE,FI , and . already , and . already
X X en_DK,SE X WORKS
X X can't find locale - -
X en_CA,GB,US,ZA WORKS WORKS
X X fr_FR WORKS WORKS
X ro_RO, dk_DK, se_SE WORKS WORKS
X ca_AD WORSK WORKS
nb_NO WORKS WORKS

So let's assume we have this class

class DecimalFormatWrapper(private val locale: Locale) {
    val decimalFormat: DecimalFormat = NumberFormat.getCurrencyInstance(locale) as DecimalFormat

    val monetaryGroupingSeparator: Char
        get() = decimalFormat.decimalFormatSymbols.monetaryGroupingSeparator

    val monetaryDecimalSeparator: Char
        get() = decimalFormat.decimalFormatSymbols.monetaryDecimalSeparator

    fun withCurrency(currencyCode: String) {
        Log.d("DF-S", "[$monetaryGroupingSeparator] / [$monetaryDecimalSeparator]")
        decimalFormat.withCurrency(Currency.getInstance(currencyCode))
        Log.d("DF-E", "[$monetaryGroupingSeparator] / [$monetaryDecimalSeparator]")
    }
}

And we use it like this:

DecimalFormatWrapper(en_DK).withCurrency("EUR")
// DF-S: [.] / [,]
// DF-E: [,] / [.]

DecimalFormatWrapper(en_DK).withCurrency("GBP")
// DF-S: [.] / [,]
// DF-E: [.] / [,]

DecimalFormatWrapper(en_SE).withCurrency("EUR")
// DF-S: [ ] / [,]
// DF-E: [,] / [.]

DecimalFormatWrapper(en_SE).withCurrency("GBP")
// DF-S: [ ] / [,]
// DF-E: [ ] / [,]

DecimalFormatWrapper(en_ZA).withCurrency("EUR")
// DF-S: [ ] / [,]
// DF-E: [ ] / [,]

DecimalFormatWrapper(ro_RO).withCurrency("EUR")
// DF-S: [.] / [,]
// DF-E: [.] / [,]

// etc

My question is: is there any reason for this behaviour, or is this a bug in the DecimalFormat/NumberFormat implementation?

tasegula
  • 889
  • 1
  • 12
  • 26

0 Answers0