Currency.getSymbol() 在不同的设备上给出 "US$" 和 "$"

Currency.getSymbol() gives "US$" and "$" with different devices

在初始化货币时,我设置了地区广告 locale.US Currency.getInstance(Locale.US),但 getSymbol() 在不同设备上给出 "US$" 和 "$。getSymbol() 在三星上给出 "$",但在 moto 上给出 "US$"。如何解决这个问题。

来自 Currency.getSymbol()

的 javadoc

Gets the symbol of this currency for the default DISPLAY locale. For example, for the US Dollar, the symbol is "$" if the default locale is the US, while for other locales it may be "US$". If no symbol can be determined, the ISO 4217 currency code is returned.

If the default DISPLAY locale contains "rg" (region override) Unicode extension, the symbol returned from this method reflects the value specified with that extension.

This is equivalent to calling getSymbol(Locale.getDefault(Locale.Category.DISPLAY)).

这描述了您有时看到“US$”而有时只看到“$”的原因。

如果您想获得可重现的结果,请使用

Currency.getInstance(Locale.US).getSymbol(Locale.US).