java - 使用ICU将拼出的数字(字符串)转换为整数

标签 java kotlin icu

我需要将字符串转换为整数。例如,我想将第六转换为6。

我使用IBM的库ICU进行了倒数(6→第六)。

private val String.spellout: String
  get() {
    val esFormatter = RuleBasedNumberFormat(Locale.ENGLISH, RuleBasedNumberFormat.SPELLOUT)
    return esFormatter.format(this.toDouble(), "%spellout-ordinal")
  }

我想创建另一个方法,该方法采用该拼写出来的字符串并将其转换为 double (第六→6)

最佳答案

从评论中获得帮助后,这是我的解决方案,以防其他人需要它:

private val String.numberFromSpelledOut: Boolean
  get() {
    val esFormatter = RuleBasedNumberFormat(Locale.ENGLISH, RuleBasedNumberFormat.SPELLOUT)
    return try {
      return esFormatter.parse(this)
    } catch (e: ParseException) {
      ""
    }
  }

对于无效的拼写输入,这不应引发异常。

关于java - 使用ICU将拼出的数字(字符串)转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55812416/

相关文章:

java - createBufferStrategy 不能作为变量?

java - Websphere Runtime事务超时问题

Kotlin 数学与数学

android - 将 viewModel 注入(inject)@Composable

spring - Autowiring 参数作为bean声明参数

sqlite - Sqlite3 ICU Extension 编译版

c++ - 不断收到错误 LNK2019 : unresolved external symbol

java - 在 Java 中实现通用接口(interface)

java - 有没有办法在语言环境中获取与给定字符等效的所有字符

java - 如何让 Avro SchemaBuilder 构建两个 'records' 之间的映射架构?