问题
Is there an easy way to read back if the language set uses a decimal comma or a decimal point?
回答1:
EDIT: Updating based on @Algar's suggestion; you can directly use:
char separatorChar = DecimalFormatSymbols.getInstance().getDecimalSeparator();
As it will always return an instance of DecimalFormatSymbols
.
NumberFormat nf = NumberFormat.getInstance();
if (nf instanceof DecimalFormat) {
DecimalFormatSymbols sym = ((DecimalFormat) nf).getDecimalFormatSymbols();
char decSeparator = sym.getDecimalSeparator();
}
Docs:
NumberFormat, DecimalFormat, DecimalFormatSymbols
According to the DecimalFormat docs, apparently calling NumberFormat.getInstance() is safe, but may return a subclass other than DecimalFormat (the other option I see is ChoiceFormat). I believe for the majority of instances it should be a DecimalFormat, and then you can compare decSeparator
against a ,
and .
to see which format it is using.
回答2:
I did try this and it worked fine...
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);
回答3:
you can use:
Currency currency = Currency.getInstance(device_locale);
than use currency.getSymbol()
for symbol. For default device locale you can use:
@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;
}
}
回答4:
Or why not
DecimalFormatSymbols.getInstance().decimalSeparator
回答5:
Not sure if there is an easy way, but you could test which language set is being used and then make the appropriate changes according to if that language used commas or decimals.
来源:https://stackoverflow.com/questions/8188137/decimal-point-or-decimal-comma-in-android