市场人员反映公司的app使用系统设置俄语、西班牙语,double数据会把小数点变为逗号。调试一下,是自定义的语言时候(例如,俄语、西班牙语)转换String.format("%.2f",67.876)。会出现的。
Android【设置】-【语言和输入法】-【语言】列表中找到相应语言所对应的列表项
java.util.Locale类
在这个Locale类里面,有些语言是没有,例如俄语、西班牙语等。那么这时候android开发时候需要这些语言,怎么办。只好后面自已新建,自定义。
/**
* Locale constant for en_CA.
*/
public static final Locale CANADA = new Locale(true, "en", "CA");
/**
* Locale constant for fr_CA.
*/
public static final Locale CANADA_FRENCH = new Locale(true, "fr", "CA");
/**
* Locale constant for zh_CN.
*/
public static final Locale CHINA = new Locale(true, "zh", "CN");
/**
* Locale constant for zh.
*/
public static final Locale CHINESE = new Locale(true, "zh", "");
/**
* Locale constant for en.
*/
public static final Locale ENGLISH = new Locale(true, "en", "");/**
* There's a circular dependency between toLowerCase/toUpperCase and
* Locale.US. Work around this by avoiding these methods when constructing
* the built-in locales.
*
* @param unused required for this constructor to have a unique signature
*/
private Locale(boolean unused, String lowerCaseLanguageCode, String upperCaseCountryCode) {
this.languageCode = lowerCaseLanguageCode;
this.countryCode = upperCaseCountryCode;
this.variantCode = "";
} /**
* Constructs a new {@code Locale} using the specified language, country,
* and variant codes.
*/
public Locale(String language, String country, String variant) {
if (language == null || country == null || variant == null) {
throw new NullPointerException();
}
if (language.isEmpty() && country.isEmpty()) {
languageCode = "";
countryCode = "";
variantCode = variant;
return;
}
languageCode = language.toLowerCase(Locale.US);
// Map new language codes to the obsolete language
// codes so the correct resource bundles will be used.
if (languageCode.equals("he")) {
languageCode = "iw";
} else if (languageCode.equals("id")) {
languageCode = "in";
} else if (languageCode.equals("yi")) {
languageCode = "ji";
}
countryCode = country.toUpperCase(Locale.US);
// Work around for be compatible with RI
variantCode = variant;
}
@Override public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError(e);
}
}private static final Locale Locale_Russia = new Locale("RUS", "ru", "");
private static final Locale Locale_Spanish = new Locale("ES", "es", "");
public static void setApplicationLauguageType(Context context, int type) {
if (context == null) return;
Resources resources = context.getResources();//获得res资源对象
Configuration config = resources.getConfiguration();//获得设置对象
DisplayMetrics dm = resources .getDisplayMetrics();//获得屏幕参数:主要是分辨率,像素等。
switch (type) {
case 0:
config.locale = Locale.getDefault();
break;
case 1:
config.locale = Locale.SIMPLIFIED_CHINESE;
break;
case 2:
config.locale = Locale.ENGLISH;
break;
case 3:
config.locale = Locale_Russia;
break;
case 4:
config.locale = Locale_Spanish;
break;
default:
config.locale = Locale.getDefault();
break;
}
resources.updateConfiguration(config, dm);
}/**
* Returns a localized formatted string, using the supplied format and arguments,
* using the user's default locale.
*
* <p>If you're formatting a string other than for human
* consumption, you should use the {@code format(Locale, String, Object...)}
* overload and supply {@code Locale.US}. See
* "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>".
*
* @param format the format string (see {@link java.util.Formatter#format})
* @param args
* the list of arguments passed to the formatter. If there are
* more arguments than required by {@code format},
* additional arguments are ignored.
* @return the formatted string.
* @throws NullPointerException if {@code format == null}
* @throws java.util.IllegalFormatException
* if the format is invalid.
* @since 1.5
*/
public static String format(String format, Object... args) {
return format(Locale.getDefault(), format, args);
}
/**
* Returns a formatted string, using the supplied format and arguments,
* localized to the given locale.
*
* @param locale
* the locale to apply; {@code null} value means no localization.
* @param format the format string (see {@link java.util.Formatter#format})
* @param args
* the list of arguments passed to the formatter. If there are
* more arguments than required by {@code format},
* additional arguments are ignored.
* @return the formatted string.
* @throws NullPointerException if {@code format == null}
* @throws java.util.IllegalFormatException
* if the format is invalid.
* @since 1.5
*/
public static String format(Locale locale, String format, Object... args) {
if (format == null) {
throw new NullPointerException("format == null");
}
int bufferSize = format.length() + (args == null ? 0 : args.length * 10);
Formatter f = new Formatter(new StringBuilder(bufferSize), locale);
return f.format(format, args).toString();
}
double dValue = 360.672;
String strValue = String.format("%.2f",dValue);结果360,672double dValue = 360.672; String strValue = String.format(Locale.ENGLISH,"%.2f",dValue);
String strResult = String.format("%s:%.3f\r\n", getString(R.string.IteInfoCoorType, 654.76);这个就要分开转换再接起来,不然都是英文,转换不了其他的语言的。
这个设计上缺陷是否是android sdk 或者jdk的存在bug。我使用都是jdk1.7和sdk 4.2版本。
欢迎大家发现与测试。
String.format("%.2f",dValue);
String.format(Locale.ENGLISH,"%.2f",dValue);android 使用String.format("%.2f",67.876)自已定义语言(俄语、西班牙语)会把小数点变为逗号
原文:http://blog.csdn.net/qq_16064871/article/details/50332473