美文网首页
Android 国家化的坑 - 小数点变成逗号

Android 国家化的坑 - 小数点变成逗号

作者: 简简单单敲代码 | 来源:发表于2018-06-05 22:50 被阅读330次

测试提【将app语言设置为印尼语时,显示的价格小数点变成了逗号】这 bug,如图:

神奇

目前 APP 有中文,英文,繁体,马来文,印尼,泰语这几个国际化,在英文中文等语言下这个 bug 是不存在的,在印尼语的情况下是存在的。英文状态下如图:


英文状态

那么可以肯定是在国家化的时候出现的bug,
先排查了一下,是在这行代码出现的问题:String.format("%.2f", price)

我们只需要修改成String.format(Locale.ENGLISH, "%.2f", price)这样就能解决这个 bug。
效果如下:

修复后

但是我们还是要看看为什么?

分析一下源码:
先看看String.format("%.2f", price)这行代码会调用

    public static String format(String format, Object... args) {
        return new Formatter().format(format, args).toString();
    }

继续跟

  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;
    }

我们注意一下这个 l 的参数,是Locale的实例,这个类位于java.util.Locale下面。
我们看看这个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是通过Locale.getDefault(Locale.Category.FORMAT)进行初始化的。

再看看String.format(Locale.ENGLISH, "%.2f", price)的调用源码。

 public static String format(Locale l, String format, Object... args) {
        return new Formatter(l).format(format, args).toString();
    }

在初始化Formatter的时候就直接传入了l这个对象。所以我们可以肯定String.format("%.2f", price)String.format(Locale.ENGLISH, "%.2f", price)最终差别在于这个Locale对象,一个是直接指明了需要的语言环境,这里是ENGLISH,一个是通过Locale.getDefault(Locale.Category.FORMAT) 去直接获取当前默认的语言环境,而在印尼语的时候,默认是印尼语。

现在知道了是由于是Locale导致的,那么就有新问题了,为什么Locale的区别会导致小数点变成了逗号?
明明是3.00结果给我整来一个3,00,这是在逗我??

黑人问号

所以我们再分析一下。在这个Locale类里面,其中会有一些默认的Useful constant for language.

这个createConstantAPI是私有的。这个方法的新建是为了匹配资源文件的翻译的以及后面的系统的调用,但是印尼语不在其中。

  /**
     * 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);
    }

总结

使用Locale类有定义,就不会出现这种情况。例如中文,英文肯定是正常的。不在Locale类定义,就会出现。

提醒

如果在使用String.format(Locale.ENGLISH, "%.2f", price)这个方法的时候,千万要记得,如果使用了string资源文件的时候,如:String.format("%s", getResources().getString(R.string.xxxx));这样写会的话那么会全是英文。
可以分开写,这样的话就不会全是英文了。

示例代码:

String s = getResources().getString(R.string.xxxx);
String.format("%s", s )

感谢你的阅读,如有问题欢迎指正!

相关文章

网友评论

      本文标题:Android 国家化的坑 - 小数点变成逗号

      本文链接:https://www.haomeiwen.com/subject/lqfvsftx.html