測(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è)類(lèi)位于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類(lèi)里面,其中會(huì)有一些默認(rèn)的Useful constant for language.

這個(gè)createConstantAPI是私有的。這個(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類(lèi)有定義,就不會(huì)出現(xiàn)這種情況。例如中文,英文肯定是正常的。不在Locale類(lèi)定義,就會(huì)出現(xiàn)。
提醒
如果在使用String.format(Locale.ENGLISH, "%.2f", price)這個(gè)方法的時(shí)候,千萬(wàn)要記得,如果使用了string資源文件的時(shí)候,如:String.format("%s", getResources().getString(R.string.xxxx));這樣寫(xiě)會(huì)的話(huà)那么會(huì)全是英文。
可以分開(kāi)寫(xiě),這樣的話(huà)就不會(huì)全是英文了。
示例代碼:
String s = getResources().getString(R.string.xxxx);
String.format("%s", s )
感謝你的閱讀,如有問(wèn)題歡迎指正!