小数点或Android的小数逗号

问题描述 投票:14回答:5

有一个简单的方法来读取回来,如果设置的语言使用十进制逗号或小数点?

android decimal-point
5个回答
21
投票

编辑:根据@阿尔加的建议更新;你可以直接使用:

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

因为它总是返回DecimalFormatSymbols的一个实例。


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

文档:

NumberFormatDecimalFormatDecimalFormatSymbols

据DecimalFormat的文档,显然调用NumberFormat.getInstance()是安全的,但可能会返回DecimalFormat的其他子类(其他选项我看到的是ChoiceFormat)。我相信对于大多数情况下,它应该是一个DecimalFormat的,然后你可以比较decSeparator针对,.看到它正在使用的格式。


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);

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;
        }
    }

1
投票

或者为什么不

DecimalFormatSymbols.getInstance().decimalSeparator

-1
投票

不知道是否有一个简单的方法,但你可以测试正在使用的语言集,然后根据如果语言使用逗号或小数,以做出相应的改变。

© www.soinside.com 2019 - 2024. All rights reserved.