【问题标题】:How to get the Locale of a NumberFormat in Java/Kotlin?如何在 Java/Kotlin 中获取 NumberFormat 的语言环境?
【发布时间】:2019-03-31 21:57:49
【问题描述】:

根据https://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.htmlNumberFormat.getInstance() 将为您提供当前语言环境中的NumberFormat 实例。我想在创建实例后验证语言环境使用getLocale() 之类的方法,但那里似乎没有记录这种方法。

我能做的最好的确定就是在 Kotlin shell (kotlinc) 中调用 Locale.getDefault()

>>> import java.math.BigDecimal
>>> import java.text.NumberFormat
>>> NumberFormat.getInstance().format(BigDecimal(1500))
1,500
>>> import java.util.Locale
>>> Locale.getDefault()
en_US

没有NumberFormat.getLocale()方法吗?

【问题讨论】:

    标签: java kotlin locale number-formatting


    【解决方案1】:

    没有NumberFormat.getLocale()方法吗?

    没有 - 你可以做的就是调用 NumberFormat.getInstance() 在后台执行的相同方法:

    Locale defaultLocale = Locale.getDefault(Locale.Category.FORMAT);
    

    Java 8 中有两个Categoryhttps://docs.oracle.com/javase/8/docs/api/java/util/Locale.Category.html。如果您需要 DISPLAY 一个,只需换掉 arg。

    【讨论】:

      猜你喜欢
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多