gpt4 book ai didi

java - 如何在没有 Android ICU 的情况下在 ChoiceFormat 中使用 "{0, ordinal}"

转载 作者:行者123 更新时间:2023-12-05 06:32:00 25 4
gpt4 key购买 nike

我正在尝试通过选择格式获取用于排行榜的英文序数。

我目前的解决方案是这样的:

{0,choice,0#th|1#st|2#nd|3#rd|3<th}

并使用这是一个参数。

int pos = position % 100 >= 11 && position % 100 <= 13 ? 0 : position % 10

这很好用,但我发现您可以将 {0,ordinal} 与 ICU 一起使用。

有什么简单的方法可以让它在没有 ICU 的情况下工作吗?

最佳答案

你可以这样使用:

import android.icu.text.MessageFormat

fun toOrdinal(day: String): String {
val formatter = MessageFormat("{0,ordinal}", Locale.getDefault())
return formatter.format(arrayOf(day.toInt()))
}

你会得到这些结果:

1 -> 1st
2 -> 2nd
3 -> 3rd
4 -> 4th
6 -> (...)

关于java - 如何在没有 Android ICU 的情况下在 ChoiceFormat 中使用 "{0, ordinal}",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51583532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com