18
votes

I'm making an Android Java program which is taking double values from the user. If I run the program on the computer, it works great because of the locale of my computer, EN_UK. But when I run it on my mobile phone with FI_FI locale, it won't work. I know the reason: In UK, people use dot as decimal separator but here in Finland, the decimal separator is comma.

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

When I'm using comma, it says java.lang.NumberFormatException: Invalid double: "1234,5".

How can I make it work with them both, comma and dot?

7
Why don't you just parse the string and replace the comma with dots if necessary?guitarflow
A not so smart solution is to first replace all the commas with dots and then treat the number as if it is formatted with dot.Ivaylo Strandjev

7 Answers

33
votes

Use one of the other constructors of DecimalFormat:

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

And then try and parse it using both separators.

4
votes

using DecimalFormatSymbols.getInstance() will produce the default locale's correct symbols, so you will get it right for any platform you run on.

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

This should work for both Java(Tested) as well as android :)

  • Class Name: 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();
        }
    
    }
    
  • Class Name: 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;
            }
        }
    }
    

Output:

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

Hope this will help somebody to make use of.

1
votes
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;
}
0
votes

Not a best way but worked for me;

    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;
-1
votes

Me Error:

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

... and this work for me

replace(",", "")
-2
votes
DecimanFormat df = new DecimalFormat("#.#");