android - NumberFormatter 函数在德语语言环境下无法正常工作

标签 android locale numberformatter

我需要根据英语和欧洲语言的语言环境添加适当的工程符号(K、M、B、T)。我认为使用 NumberFormatter建议用于 Android API 30+。

我已经实现了这个功能来进行转换,对于英语和其他欧盟语言(除了德语之外)都可以顺利进行,这很奇怪。我已经针对西类牙语、法语和意大利语进行了测试。

我需要帮助找出它不适用于德语的原因,因为德语对于我的案例支持来说是非常重要的语言。

/**
 * 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()

最佳答案

也许这是语言本身特有的。例如,我认为在德语中,没有像K这样的字符来表示一千。甚至一万。 同样,在日本,没有千的表示,但有万。以下是一些示例

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

如果您注意到,即使没有字符添加为数字值的后缀,但始终有一位小数。这就是你的代码的 Intent 。仅在德国,点 (.) 用逗号 (,) 反转。

关于android - NumberFormatter 函数在德语语言环境下无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75987645/

相关文章:

java - JDK 7 的支持终止对 Android 开发有何影响

android - ViewModel 将 ArrayList 大小返回为零

android - Cordova 构建错误 - 无法找到属性 android :font

以 UTF-8 编码读取文本

java - Asynctask类中的静态变量用还是反对?

java - 如何从java中的编码中查找语言环境

linux - 如何检查区域设置是否为 UTF-8?

php - 使用 NumberFormatter 仅获取序数后缀

PHP 5.3.0 NumberFormatter 替换

swift - 本地化不同语言的数字整数