【发布时间】:2019-09-12 17:02:09
【问题描述】:
我需要将字符串转换为整数。例如,我想将 6 转换为 6。
我使用 IBM 的库 ICU 进行了相反的操作(6 → 第六)。
private val String.spellout: String
get() {
val esFormatter = RuleBasedNumberFormat(Locale.ENGLISH, RuleBasedNumberFormat.SPELLOUT)
return esFormatter.format(this.toDouble(), "%spellout-ordinal")
}
我想创建另一种方法,该方法采用该拼写字符串并将其转换为双精度(第六→ 6)
【问题讨论】:
-
你试过icu-project.org/apiref/icu4j/com/ibm/icu/text/…,例如
val esFormatter = RuleBasedNumberFormat(Locale.ENGLISH, RuleBasedNumberFormat.ORDINAL); val n = esFormatter.parse("sixth") -
RuleBasedNumberFormat也有一个parse方法,看起来它正在做你想做的事。 -
你们是对的。我试过了,但也将它用于无效字符串,所以它抛出了异常。我不得不用 try-catch 来包装它。