Doubles, commas and dots

后端 未结 7 1205
深忆病人
深忆病人 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: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.

提交回复
热议问题