NumberFormatter function not working correctly with German Locale

175 Views Asked by At

I needed to add the appropriate engineering notations (K, M, B, T) as per Locale for English and European lanuages. I figured that using NumberFormatter is suggested for Android API 30+.

I have implemented this function to do the conversion which works smooth for English and other EU languages except German which is very strange. I have tested against Spanish, French and Italian.

I need help in figuring out what could be the reason it is not working for German as it is highly important language for my case to support.

/**
 * Converts Number to appropriate rounded off format with max 1 decimal place
 * e.g. 1345 --> 1.3K, 15555900 --> 15.6M
*/
private fun formatCount(number: Long) = NumberFormatter.with()
    .notation(Notation.compactShort())
    .decimal(NumberFormatter.DecimalSeparatorDisplay.ALWAYS)
    .precision(Precision.fixedFraction(1))
    .locale(Resources.getSystem().configuration.locales.get(0))
    .format(number)
    .toString()
1

There are 1 best solutions below

1
S.Javed On BEST ANSWER

Maybe that is specific to the Language itself. For example, in German, I think, there is no representation of a thousand with a character like K. or even for ten thousand. Similarly in Japan, there is no representation for a thousand but there is for 10 thousand . Below are some examples

Locale.GERMANY
----------------
15 -> 15,0
135 -> 135,0
1345 -> 1345,0
13455 -> 13.455,0
134555 -> 134.555,0
1345555 -> 1,3 Mio.
15555900 -> 15,6 Mio.
155559000 -> 155,6 Mio.
1555590000 -> 1,6 Mrd.

Locale.JAPAN
-------------
15 -> 15.0
135 -> 135.0
1345 -> 1345.0
13455 -> 1.3万
134555 -> 13.5万
1345555 -> 134.6万
15555900 -> 1555.6万
155559000 -> 1.6億
1555590000 -> 15.6億

Locale.FRANCE
---------------
15 -> 15,0
135 -> 135,0
1345 -> 1,3 k
13455 -> 13,5 k
134555 -> 134,6 k
1345555 -> 1,3 M
15555900 -> 15,6 M
155559000 -> 155,6 M
1555590000 -> 1,6 Md

If you notice even though there are no characters added as a suffix to the number value but there is always a single-digit decimal. Which is the intention of your code. Only in Germany, the point(.) is reversed with a comma (,).