Doubles, commas and dots

后端 未结 7 1174
深忆病人
深忆病人 2020-12-14 09:03

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, E

相关标签:
7条回答
  • 2020-12-14 09:25

    Me Error:

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

    ... and this work for me

    replace(",", "")
    
    0 讨论(0)
  • 2020-12-14 09:28

    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;
    
    0 讨论(0)
  • 2020-12-14 09:31

    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());
    
    0 讨论(0)
  • 2020-12-14 09:36
    DecimanFormat df = new DecimalFormat("#.#");
    
    0 讨论(0)
  • 2020-12-14 09:38

    Use one of the other constructors of DecimalFormat:

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

    And then try and parse it using both separators.

    0 讨论(0)
  • 2020-12-14 09:40

    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.

    0 讨论(0)
提交回复
热议问题