有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用ICU将输出数字(字符串)拼写为整数

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

我做了相反的一个→ 第六)使用IBM的图书馆ICU

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

我想创建另一个方法,该方法使用拼写好的字符串并将其转换为双精度(第六个)→ (六)


共 (1) 个答案

  1. # 1 楼答案

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

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

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