双打,逗号和圆点

时间:2012-05-14 14:06:55

标签: java android decimal

我正在制作一个从用户那里获取双倍价值的Android Java程序。如果我在计算机上运行该程序,它可以很好地工作,因为我的计算机的区域设置EN_UK。但是当我在带有FI_FI语言环境的手机上运行时,它将无法正常工作。我知道原因:在英国人使用点作为十进制分隔符,但在芬兰这里的十进制分隔符是逗号。

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

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

如何让它们兼具逗号和点?

7 个答案:

答案 0 :(得分:32)

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

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

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

答案 1 :(得分:4)

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

DecimalFormat df = new DecimalFormat("#.#", DecimalFormatSymbols.getInstance());

答案 2 :(得分:2)

这应该适用于Java(已测试)以及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

希望这会有助于某人使用。

答案 3 :(得分: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;
}

答案 4 :(得分:0)

不是最好的方式,但为我工作;

    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;

答案 5 :(得分:-1)

我错误:

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

......这对我有用

replace(",", "")

答案 6 :(得分:-2)

DecimanFormat df = new DecimalFormat("#.#");