我需要将字符串转换为整数。例如,我想将第六个转换为6个。
我使用IBM的库ICU做了相反的(6→6)。
private val String.spellout: String
get() {
val esFormatter = RuleBasedNumberFormat(Locale.ENGLISH, RuleBasedNumberFormat.SPELLOUT)
return esFormatter.format(this.toDouble(), "%spellout-ordinal")
}
我想创建另一个方法来获取拼写出来的字符串并将其转换为双精度(第六→6)
在获得评论的帮助后,这是我的解决方案,万一其他人需要它:
private val String.numberFromSpelledOut: Boolean
get() {
val esFormatter = RuleBasedNumberFormat(Locale.ENGLISH, RuleBasedNumberFormat.SPELLOUT)
return try {
return esFormatter.parse(this)
} catch (e: ParseException) {
""
}
}
这不应该为无效的拼写输入抛出异常。