2011-11-18 142 views

回答

14

,我相信我有办法。我从来没有用过这个,你要知道,但肯定应该让你关闭,如果不是有:

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针对,.看到它正在使用的格式。

+0

感谢这个伟大工程 – patrick

+2

我需要投'nf'到''DecimalFormat'((DecimalFormat的)NF).getDecimalFormatSymbols()',非常感谢。 –

+0

谢谢@SelçukChan,我编辑了答案,包括必要的演员! – kcoppock

-1

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

+0

语言和小数点分隔符是两个不同的问题。另外,你不能列出所有现有的语言。 – Loda

1

我曾尝试这一点,它工作得很好......

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; 
     } 
    } 
相关问题