android 使用String.format(%.2f,67.876)自已定义语言(俄语、西班牙语)会把小数点变为逗号...
市場人員反映公司的app使用系統(tǒng)設(shè)置俄語、西班牙語,double數(shù)據(jù)會把小數(shù)點變?yōu)槎禾枴U{(diào)試一下,是自定義的語言時候(例如,俄語、西班牙語)轉(zhuǎn)換String.format("%.2f",67.876)。會出現(xiàn)的。
1、android 系統(tǒng),設(shè)置系統(tǒng)語言的步驟
Android【設(shè)置】-【語言和輸入法】-【語言】列表中找到相應(yīng)語言所對應(yīng)的列表項
?
2、問題分析
java.util.Locale類
在這個Locale類里面,有些語言是沒有,例如俄語、西班牙語等。那么這時候android開發(fā)時候需要這些語言,怎么辦。只好后面自已新建,自定義。
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** ?* 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", ""); |
Locale類里面,私有方法新建語言的。可是不提供外部調(diào)用。
?
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 | /** ?????* 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 = ""; ????} |
源碼中的這個方法是共外部新建語言的。
構(gòu)造一個新的{ @code地區(qū)}使用指定的語言,國家,和變體編碼。
?
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /** ?* 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); ????} } |
這是我在外面新建俄語、西班牙語。因為調(diào)用不了上面是有方法。只有這個方法。
這個方法的新建是為了匹配資源文件的翻譯的以及后面的系統(tǒng)的調(diào)用
?
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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();//獲得設(shè)置對象 ????????DisplayMetrics dm = resources .getDisplayMetrics();//獲得屏幕參數(shù):主要是分辨率,像素等。 ????????? ????????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); ????} |
?
?| 1 2 3 4 5 | /** ?????* Returns a localized formatted string, using the supplied format and arguments, ?????* using the user's default locale. ?????* ?????* |
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 * "Be wary of the default locale". * * @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); }
?
?
這上面是一般調(diào)用方法,String strResult = String.format("%0.2f",543.6356);
這句語句是double型543.6356保留小數(shù)點的兩位,并轉(zhuǎn)化成String類型。
調(diào)用這個方法時候,其實是默認(rèn)調(diào)用format(Locale.getDefault(), format, args)。因為返回是調(diào)用了這個。多了一個參數(shù)就是Locale.getDefault()。這個參數(shù)使獲取系統(tǒng)設(shè)置的語言。并作為后面的轉(zhuǎn)換參數(shù)的。
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /** ??* 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(); ?} |
那么現(xiàn)在問題就來了。但我使用自定義的語言時候(例如,俄語、西班牙語)。使用下面語句轉(zhuǎn)換,小數(shù)點會變成逗號
?
?
?| 1 2 | double dValue = 360.672; String strValue = String.format("%.2f",dValue); |
結(jié)果360,672
如果我使用Locale類有定義,就不會出現(xiàn)這種情況。例如中文,英文肯定是正常的。
?
3、解決辦法
分析到這里了,就說一下解決辦法。
只好調(diào)用這個方法String.format(Locale.ENGLISH,"%.2f",dValue),多了一個參數(shù)。自已傳進(jìn)去轉(zhuǎn)換的語言是什么。不再是默認(rèn)系統(tǒng)語言設(shè)置的。因為對于數(shù)據(jù),全世界都是通用,那么默認(rèn)使用英語來轉(zhuǎn)換都沒有問題。數(shù)據(jù)都是這樣轉(zhuǎn)換的。
?
?
?| 1 2 | double dValue = 360.672; String strValue = String.format(Locale.ENGLISH,"%.2f",dValue); |
那么又產(chǎn)生一個問題,同時有轉(zhuǎn)換字符串。這個得注意一下。
| 1 | String strResult = String.format("%s:%.3f\r\n", getString(R.string.IteInfoCoorType, 654.76); |
這個就要分開轉(zhuǎn)換再接起來,不然都是英文,轉(zhuǎn)換不了其他的語言的。
?
?
這個設(shè)計上缺陷是否是android sdk 或者jdk的存在bug。我使用都是jdk1.7和sdk 4.2版本。
歡迎大家發(fā)現(xiàn)與測試。
?
?| 1 2 | String.format("%.2f",dValue); String.format(Locale.ENGLISH,"%.2f",dValue); |
轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/5453865.html
總結(jié)
以上是生活随笔為你收集整理的android 使用String.format(%.2f,67.876)自已定义语言(俄语、西班牙语)会把小数点变为逗号...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 链表——逆置
- 下一篇: 【海洋女神原创】谈谈静默安装