18

我正在制作一个从用户那里获取双值的 Android Java 程序。如果我在计算机上运行该程序,由于我计算机的语言环境 EN_UK,它运行良好。但是当我用 FI_FI 语言环境在我的手机上运行它时,它就不起作用了。我知道原因:在英国,人们使用点作为小数分隔符,但在芬兰,小数分隔符是逗号。

DecimalFormat df = new DecimalFormat("#.#");
Double returnValue = Double.valueOf(df.format(doubleNumber));

当我使用逗号时,它说java.lang.NumberFormatException: Invalid double: "1234,5".

我怎样才能让它与他们一起工作,逗号和点?

4

7 回答 7

33

使用 DecimalFormat 的其他构造函数之一:

new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.US))

然后尝试使用两个分隔符解析它。

于 2012-05-14T14:12:32.383 回答
4

usingDecimalFormatSymbols.getInstance() 将生成默认语言环境的正确符号,因此您可以在运行的任何平台上正确使用它。

DecimalFormat df = new DecimalFormat("#.#", DecimalFormatSymbols.getInstance());
于 2012-05-14T14:15:29.430 回答
2

这应该适用于 Java(Tested) 和 android :)

  • 类名:In18Helper.java

    package com.akmeher.app.utils;
    
    import java.text.NumberFormat;
    import java.text.ParseException;
    import java.util.Locale;
    
    public class In18Helper {
        private final static In18Helper mHelper = new In18Helper();
    
        public static final In18Helper getInstance() {
            return mHelper;
        }
    
        public double getDouble(String sValue, Locale locale) {
            NumberFormat numberFormat = NumberFormat.getInstance(locale);
    
            Number parse = null;
            try {
                parse = numberFormat.parse(sValue);
            } catch (ParseException e) {
                e.printStackTrace();
            }
    
            return parse == null ? 0 : parse.doubleValue();
        }
    
    }
    
  • 类名:Application.java

    package com.akmeher.app;
    
    import java.util.Locale;
    import com.akmeher.app.utils.In18Helper;
    
    public class Application {
    
        static DataModel[] testData = new DataModel[] {
                new DataModel("1.034567", Locale.ENGLISH),
                new DataModel("1,0345.67", Locale.ENGLISH),
                new DataModel("1.0345,67", Locale.GERMANY),
                new DataModel("1,034,567", Locale.CANADA),
                new DataModel("1.034567", Locale.KOREA),
                new DataModel("1,03.4567", Locale.ITALY) };
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            for (int i = 0; i < testData.length; i++) {
                        double d = In18Helper.getInstance().getDouble(testData[i].mValue,
                        testData[i].mLocale);
    
                System.out.println("Trial Value: "+testData[i].mValue+" for Locale: "+testData[i].mLocale+" converted to: "+d);
            }
        }
    
        private static class DataModel {
            String mValue;
            Locale mLocale;
    
            public DataModel(String value, Locale locale) {
                this.mLocale = locale;
                this.mValue = value;
            }
        }
    }
    

输出:

Trial Value: 1.034567 for Locale: en converted to: 1.034567 Trial Value: 1,0345.67 for Locale: en converted to: 10345.67 Trial Value: 1.0345,67 for Locale: de_DE converted to: 10345.67 Trial Value: 1,034,567 for Locale: en_CA converted to: 1034567.0 Trial Value: 1.034567 for Locale: ko_KR converted to: 1.034567 Trial Value: 1,03.4567 for Locale: it_IT converted to: 1.03

希望这将有助于有人使用。

于 2015-04-10T08:01:07.977 回答
1
public static Double parseDoubleTL(String value){
    DecimalFormat df =  new DecimalFormat("#.#", new DecimalFormatSymbols(new Locale("tr_TR")));
    Double doublePrice = 0.0;
    try {
        doublePrice =  df.parse(value).doubleValue();
    } catch (ParseException e) {
        Log.w(MainActivity.TAG,"Couldnt parse TL. Error is "+e.toString());
    }
    return doublePrice;
}
于 2014-05-07T19:39:04.073 回答
-1

不是最好的方法,但对我有用;

    Double val=null;
    try{
        val=Double.valueOf(value);
    }catch(Exception e){
        val=Double.valueOf(value.replace(',','.'));
    }
            Double val=null;
    try{
        val=Double.valueOf(value);
    }catch(Exception e){
        val=Double.valueOf(value.replace(',','.'));
    }
    return val;
于 2014-06-26T17:35:53.110 回答
-1

我的错误:

java.lang.NumberFormatException: Invalid float: "1,683.88"

...这对我有用

replace(",", "")
于 2016-12-14T14:56:41.897 回答
-2
DecimanFormat df = new DecimalFormat("#.#");
于 2013-02-12T01:55:52.957 回答