如何在 Java/Kotlin 中获取 NumberFormat 的区域设置?

How to get the Locale of a NumberFormat in Java/Kotlin?

根据 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()方法吗?

Is there no NumberFormat.getLocale() method?

没有 - 你可以做什么,但调用与 NumberFormat.getInstance() 相同的方法:

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

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