市场人员反映公司的app使用系统设置俄语、西班牙语,double数据会把小数点变为逗号。调试一下,是自定义的语言时候(例如,俄语、西班牙语)转换String.format("%.2f",67.876)。会出现的。

1、android 系统,设置系统语言的步骤

Android【设置】-【语言和输入法】-【语言】列表中找到相应语言所对应的列表项

2、问题分析

java.util.Locale类

在这个Locale类里面,有些语言是没有,例如俄语、西班牙语等。那么这时候android开发时候需要这些语言,怎么办。只好后面自已新建,自定义。

?
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类里面,私有方法新建语言的。可是不提供外部调用。

?
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 = "";
    }

源码中的这个方法是共外部新建语言的。
构造一个新的{ @code地区}使用指定的语言,国家,和变体编码。

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

这是我在外面新建俄语、西班牙语。因为调用不了上面是有方法。只有这个方法。
这个方法的新建是为了匹配资源文件的翻译的以及后面的系统的调用

?
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();//获得设置对象
        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);
    }

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

这上面是一般调用方法,String strResult = String.format("%0.2f",543.6356);
这句语句是double型543.6356保留小数点的两位,并转化成String类型。
调用这个方法时候,其实是默认调用format(Locale.getDefault(), format, args)。因为返回是调用了这个。多了一个参数就是Locale.getDefault()。这个参数使获取系统设置的语言。并作为后面的转换参数的。

?
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();
 }

那么现在问题就来了。但我使用自定义的语言时候(例如,俄语、西班牙语)。使用下面语句转换,小数点会变成逗号

?
1
2
double dValue = 360.672;
String strValue = String.format("%.2f",dValue);

结果360,672

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

3、解决办法

分析到这里了,就说一下解决办法。
只好调用这个方法String.format(Locale.ENGLISH,"%.2f",dValue),多了一个参数。自已传进去转换的语言是什么。不再是默认系统语言设置的。因为对于数据,全世界都是通用,那么默认使用英语来转换都没有问题。数据都是这样转换的。

?
1
2
double dValue = 360.672;
String strValue = String.format(Locale.ENGLISH,"%.2f",dValue);

那么又产生一个问题,同时有转换字符串。这个得注意一下。

?
1
String strResult = String.format("%s:%.3f\r\n", getString(R.string.IteInfoCoorType, 654.76);

这个就要分开转换再接起来,不然都是英文,转换不了其他的语言的。

这个设计上缺陷是否是android sdk 或者jdk的存在bug。我使用都是jdk1.7和sdk 4.2版本。
欢迎大家发现与测试。

?
1
2
String.format("%.2f",dValue);
String.format(Locale.ENGLISH,"%.2f",dValue);

转载于:https://www.cnblogs.com/Free-Thinker/p/5453865.html

android 使用String.format(%.2f,67.876)自已定义语言(俄语、西班牙语)会把小数点变为逗号...相关推荐

  1. android 解决String.format多语言存在的问题

    今天,简单讲讲android里如何解决String.format在切换语言时存在的问题. 之前,我写过一篇博客,讲关于String.format的基本使用,大家如果没有看过,建议看看或者去网上查找资料 ...

  2. android string.format()长度,Android通过String.format格式化(动态改变)字符串资源的显示内容...

    一.实现效果: 最近在项目中需要做类似于上图显示的效果,里面的数字和称谓是动态获取的,对于这种显示效果,有如下两种解决方案来处理: (1)通过代码动态设置TextView的内容,比如: /** * 显 ...

  3. 我的Android进阶之旅------Java字符串格式化方法String.format()格式化float型时小数点变成逗号问题...

    今天接到一个波兰的客户说有个APP在英文状态下一切运行正常,但是当系统语言切换到波兰语言的时候,程序奔溃了.好吧,又是我来维护. 好吧,先把系统语言切换到波兰语,切换到波兰语的方法查看文章 我的And ...

  4. java里format报错,我的Android进阶之旅------Java字符串格式化方法String.format()格式化float型时小数点变成逗号问题...

    今天接到一个波兰的客户说有个APP在英文状态下一切运行正常,但是当系统语言切换到波兰语言的时候,程序奔溃了.好吧,又是我来维护. 好吧,先把系统语言切换到波兰语,切换到波兰语的方法查看文章 地址:ht ...

  5. String.format()的使用

    今天,我觉得自己其实也没有什么可以写的东西,不过还是为了保持写博客的习惯,所以在网上找了一些内容.其实也很简单.大家有兴趣可以自己去查找资料. 常规类型的格式化 String类的format()方法用 ...

  6. JAVA字符串格式化-String.format()的使用

    常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...

  7. java format 字符_JAVA字符串格式化-String.format()的使用

    常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...

  8. JAVA String.format 方法使用介绍

    在JDK1.5中,String类增加了静态方法format(String format, Objects... args),format(Local l ,String format, Objects ...

  9. JAVA String format 方法使用介绍

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 在JDK ...

最新文章

  1. python 打印异常内容_python打印异常信息的两种实现方式
  2. 双击SDK Manager.exe和AVD Manager.exe时,弹出提示:failed to execute tools\android.bat解决办法
  3. 使用Docker Swarm部署MinIO ​​​​​​​
  4. Android(java)学习笔记114:Service生命周期
  5. 全年CCF级别会议列表
  6. 利用Netica训练简易贝叶斯网络模型【教程】
  7. php汉字转换拼音插件,汉字转换拼音的PHP库
  8. 服务器端jQuery – phpQuery简要
  9. JAVA NIO介绍及使用
  10. 飞凌小课堂-RK3399系列 linux双千兆网口解决方法-RTL8153
  11. Android 在现有项目中引入Compose
  12. PHP之父Rasmus Lerdorf演讲:激情下的PHP 百作坊
  13. 更改Ubuntu 18.04的时区
  14. 使用uniapp实现全局悬浮按钮(可拖动)
  15. 全媒体时代的速度与激情:香港凤凰卫视云端转型实践
  16. # TIG监控体系搭建
  17. Docker提交天池比赛流程
  18. Scala学习笔记(2)-基础语法
  19. 渐变背景(background)效果
  20. 换一种姿势挖掘任意用户密码重置漏洞

热门文章

  1. 【Linux系统编程】Linux进程管理
  2. 【Protocol Buffer】Protocol Buffer入门教程(四):序列化和反序列化
  3. 【Linux网络】Linux Socket编程 TCP协议
  4. html文件语言表示网页标题,HTML网页基本结构(HTML文件、编程语言)——十一号笔记...
  5. java 加减乘除 工具类_Java数学工具类MathUtil详解
  6. 微信小程序 列表的分页实现(最新的最简易的实现方式+思路,附代码)
  7. beandefinition与beanfactory
  8. hadoop(4)——用python代码结合hadoop完成一个小项目
  9. PHP 使用 AES/ECB/PKCS7 padding 加密
  10. linux命令查找行数命令,Linux中用grep命令来搜索单词及统计匹配的行数