24

如果语言集使用小数逗号或小数点,是否有一种简单的回读方法?

4

5 回答 5

31

编辑:根据@Algar 的建议进行更新;你可以直接使用:

char separatorChar = DecimalFormatSymbols.getInstance().getDecimalSeparator();

因为它总是会返回一个DecimalFormatSymbols.


NumberFormat nf = NumberFormat.getInstance();
if (nf instanceof DecimalFormat) {
    DecimalFormatSymbols sym = ((DecimalFormat) nf).getDecimalFormatSymbols();
    char decSeparator = sym.getDecimalSeparator();
}

文件:

NumberFormat, DecimalFormat,DecimalFormatSymbols

根据 DecimalFormat 文档,显然调用 NumberFormat.getInstance() 是安全的,但可能会返回 DecimalFormat 以外的子类(我看到的另一个选项是 ChoiceFormat)。我相信在大多数情况下,它应该是 DecimalFormat,然后您可以decSeparator与 a进行比较,.看看它使用的是哪种格式。

于 2011-11-18T19:51:36.437 回答
5

或者为什么不

DecimalFormatSymbols.getInstance().decimalSeparator
于 2018-09-30T19:08:39.303 回答
2

我确实试过这个,它工作得很好......

    String osVersion = System.getProperty("os.version");
String PhoneModel = android.os.Build.MODEL;
String locale = this.getResources().getConfiguration().locale.getDisplayCountry();
char decSeparator = '*';
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
decSeparator = dfs.getDecimalSeparator();
String androidVersion = android.os.Build.VERSION.RELEASE;
String prologue = String.format("OS verson = %s PhoneModel = %s locale = %s DecimalFormatSymbol = [%c] androidVersion = %s ", 
        osVersion ,PhoneModel, locale, decSeparator,androidVersion);
于 2011-12-23T11:59:38.640 回答
1

您可以使用:

Currency currency = Currency.getInstance(device_locale);

而不是currency.getSymbol()用于符号。对于默认设备区域设置,您可以使用:

@TargetApi(Build.VERSION_CODES.N)
    public static Locale getCurrentLocale(Context c) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return c.getResources().getConfiguration().getLocales().get(0);
        } else {
            //noinspection deprecation
            return c.getResources().getConfiguration().locale;
        }
    }
于 2017-08-02T12:50:11.140 回答
-1

不确定是否有简单的方法,但您可以测试正在使用哪种语言集,然后根据该语言是否使用逗号或小数进行适当的更改。

于 2011-11-18T19:45:25.513 回答