測(cè)試提【將app語(yǔ)言設(shè)置為印尼語(yǔ)時(shí),顯示的價(jià)格小數(shù)點(diǎn)變成了逗號(hào)】這 bug,如圖:
目前 APP 有中文轨淌,英文迂烁,繁體,馬來(lái)文递鹉,印尼盟步,泰語(yǔ)這幾個(gè)國(guó)際化,在英文中文等語(yǔ)言下這個(gè) bug 是不存在的躏结,在印尼語(yǔ)的情況下是存在的却盘。英文狀態(tài)下如圖:
那么可以肯定是在國(guó)家化的時(shí)候出現(xiàn)的bug,
先排查了一下媳拴,是在這行代碼出現(xiàn)的問(wèn)題:String.format("%.2f", price)
我們只需要修改成String.format(Locale.ENGLISH, "%.2f", price)
這樣就能解決這個(gè) bug黄橘。
效果如下:
但是我們還是要看看為什么?
分析一下源碼:
先看看String.format("%.2f", price)
這行代碼會(huì)調(diào)用
public static String format(String format, Object... args) {
return new Formatter().format(format, args).toString();
}
繼續(xù)跟
public Formatter format(String format, Object ... args) {
return format(l, format, args);
}
public Formatter format(Locale l, String format, Object ... args) {
ensureOpen();
// index of last argument referenced
int last = -1;
// last ordinary index
int lasto = -1;
FormatString[] fsa = parse(format);
for (int i = 0; i < fsa.length; i++) {
FormatString fs = fsa[i];
int index = fs.index();
try {
switch (index) {
case -2: // fixed string, "%n", or "%%"
fs.print(null, l);
break;
case -1: // relative index
if (last < 0 || (args != null && last > args.length - 1))
throw new MissingFormatArgumentException(fs.toString());
fs.print((args == null ? null : args[last]), l);
break;
case 0: // ordinary index
lasto++;
last = lasto;
if (args != null && lasto > args.length - 1)
throw new MissingFormatArgumentException(fs.toString());
fs.print((args == null ? null : args[lasto]), l);
break;
default: // explicit index
last = index - 1;
if (args != null && last > args.length - 1)
throw new MissingFormatArgumentException(fs.toString());
fs.print((args == null ? null : args[last]), l);
break;
}
} catch (IOException x) {
lastException = x;
}
}
return this;
}
我們注意一下這個(gè) l
的參數(shù)禀挫,是Locale
的實(shí)例旬陡,這個(gè)類位于java.util.Locale
下面。
我們看看這個(gè)l
的初始化语婴。
/**
* Constructs a new formatter.
*
* <p> The destination of the formatted output is a {@link StringBuilder}
* which may be retrieved by invoking {@link #out out()} and whose
* current content may be converted into a string by invoking {@link
* #toString toString()}. The locale used is the {@linkplain
* Locale#getDefault(Locale.Category) default locale} for
* {@linkplain Locale.Category#FORMAT formatting} for this instance of the Java
* virtual machine.
*/
public Formatter() {
this(Locale.getDefault(Locale.Category.FORMAT), new StringBuilder());
}
/* Private constructors */
private Formatter(Locale l, Appendable a) {
this.a = a;
this.l = l;
this.zero = getZero(l);
}
這里是l
是通過(guò)Locale.getDefault(Locale.Category.FORMAT)
進(jìn)行初始化的描孟。
再看看String.format(Locale.ENGLISH, "%.2f", price)
的調(diào)用源碼。
public static String format(Locale l, String format, Object... args) {
return new Formatter(l).format(format, args).toString();
}
在初始化Formatter
的時(shí)候就直接傳入了l
這個(gè)對(duì)象砰左。所以我們可以肯定String.format("%.2f", price)
和String.format(Locale.ENGLISH, "%.2f", price)
最終差別在于這個(gè)Locale
對(duì)象匿醒,一個(gè)是直接指明了需要的語(yǔ)言環(huán)境,這里是ENGLISH
缠导,一個(gè)是通過(guò)Locale.getDefault(Locale.Category.FORMAT)
去直接獲取當(dāng)前默認(rèn)的語(yǔ)言環(huán)境廉羔,而在印尼語(yǔ)的時(shí)候,默認(rèn)是印尼語(yǔ)僻造。
現(xiàn)在知道了是由于是Locale
導(dǎo)致的憋他,那么就有新問(wèn)題了,為什么Locale
的區(qū)別會(huì)導(dǎo)致小數(shù)點(diǎn)變成了逗號(hào)髓削?
明明是3.00
結(jié)果給我整來(lái)一個(gè)3,00
竹挡,這是在逗我?立膛?
所以我們?cè)俜治鲆幌戮竞薄T谶@個(gè)Locale
類里面,其中會(huì)有一些默認(rèn)的Useful constant for language.
這個(gè)createConstant
API是私有的宝泵。這個(gè)方法的新建是為了匹配資源文件的翻譯的以及后面的系統(tǒng)的調(diào)用好啰,但是印尼語(yǔ)不在其中。
/**
* This method must be called only for creating the Locale.*
* constants due to making shortcuts.
*/
private static Locale createConstant(String lang, String country) {
BaseLocale base = BaseLocale.createInstance(lang, country);
return getInstance(base, null);
}
總結(jié)
使用Locale類有定義儿奶,就不會(huì)出現(xiàn)這種情況框往。例如中文,英文肯定是正常的闯捎。不在Locale類定義椰弊,就會(huì)出現(xiàn)嘁酿。
提醒
如果在使用String.format(Locale.ENGLISH, "%.2f", price)
這個(gè)方法的時(shí)候,千萬(wàn)要記得男应,如果使用了string
資源文件的時(shí)候,如:String.format("%s", getResources().getString(R.string.xxxx));
這樣寫會(huì)的話那么會(huì)全是英文娱仔。
可以分開寫沐飘,這樣的話就不會(huì)全是英文了。
示例代碼:
String s = getResources().getString(R.string.xxxx);
String.format("%s", s )
感謝你的閱讀牲迫,如有問(wèn)題歡迎指正耐朴!