【问题标题】:Method to add two values of type float and integer in AndroidAndroid中float和integer类型两个值相加的方法
【发布时间】:2013-12-11 06:55:23
【问题描述】:

Android小应用,执行addition等基本操作,OnClick()如下

@Override
public void onClick(View v) {
    switch (v.getId()) {
           case R.id.btnAdd: 
           isValidToProcess(1);
           break;
           ......
           /*Switch continues for all other operations like Subtraction,etc*/
    }
}

而我的isValidToProcess()如下

private boolean isValidToProcess(int a) {
    String num1 = mEdit1.getText().toString();
    String num2 = mEdit2.getText().toString();

    if (num1.matches("") || num2.matches("")) 
    {
        ValueEmptyWarning();
    }
    else {
        float numa = Float.parseFloat(num1);
        float numb = Float.parseFloat(num2);
        switch (a) {

        case 1:
            addition(numa, numb);
            break;
              ......
           /*Switch continues for all other operations like Subtraction,etc*/
    }
}

我的addition() 函数

public void addition(float numa, float numb) {
    answer = numa + numb;
    mEdit3.setText(String.valueOf(answer));
    Log.v(TAG, "Error at Subtraction");
}

这个程序对 Float 和 Integer 数字工作正常,但问题是,对于整数和浮点值,答案都是分数,例如 Number1=2 和 Number2=3 和 answer=5.0

目的:如果用户输入整数,小数点不应该在那里。

这是否可以获得用户在EditText 上输入的值的类型?

【问题讨论】:

  • 在调用加法方法之前检查输入的值是整数还是浮点数表示numa和numb值

标签: android casting integer android-edittext floating


【解决方案1】:

首先检查 Integer.parseInt(numer) 并捕获 NumberFormatException 。如果它会正确解析它,那么它是一个整数,否则你可以使用浮点数。

【讨论】:

    【解决方案2】:

    如需更多控制,请尝试重载方法的基本 OOP 概念,例如

    public float addition(float numa, float numb) {
        // will return float
        return numa + numb;
    }
    
    public int addition(int numa, float numb) {
        // explicitly cast to int
        return numa + (int) numb;
    }
    
    public int addition(float numa, int numb) {
        // explicitly cast to int
        return (int) numa + numb;
    }
    
    public int addition(int numa, int numb) {
        // will return int
        return numa + numb;
    }
    

    要检查您的输入,请尝试以下操作...

    public void examineInput(String input1, String input2) {
    
        // For both are float
        if (input1.indexOf(".") != -1 && input2.indexOf(".") != -1) {
            float numa = Float.parseFloat(input1);
            float numb = Float.parseFloat(input2);
            float ans = addition(numa, numb);
            Log.i(TAG, String.format("%f + %f = %f", numa, numb, ans));
        }
    
        // for first to be int and second to be float
        else if (input1.indexOf(".") == -1 && input2.indexOf(".") != -1) {
            int numa = Integer.parseInt(input1);
            float numb = Float.parseFloat(input2);
            int ans = addition(numa, numb);
            Log.i(TAG, String.format("%d + %f = %d", numa, numb, ans));
        }
    
        // for first to be float and second to be int
        else if (input1.indexOf(".") != -1 && input2.indexOf(".") == -1) {
            float numa = Float.parseFloat(input1);
            int numb = Integer.parseInt(input2);
            int ans = addition(numa, numb);
            Log.i(TAG, String.format("%f + %d = %d", numa, numb, ans));
        }
    
        // for both to be int
        else if (input1.indexOf(".") == -1 && input2.indexOf(".") == -1) {
            int numa = Integer.parseInt(input1);
            int numb = Integer.parseInt(input2);
            int ans = addition(numa, numb);
            Log.i(TAG, String.format("%d + %d = %d", numa, numb, ans));
        }
    }
    

    是测试此代码的输入,带有输出

    examineInput("5.2", "6.2");    // 5.200000 + 6.200000 = 11.400000
    examineInput("5", "3.6");      // 5 + 3.600000 = 8
    examineInput("1.6", "5");      // 1.600000 + 5 = 6
    examineInput("5", "5");        // 5 + 5 = 10
    

    注意:您需要验证 ExamInput 始终获取有效数字,而不是非数字字符串...

    希望这也有助于改进 OOP 概念..:)

    【讨论】:

    • 我同意,但是我们从这些语句String num1 = mEdit1.getText().toString(); String num2 = mEdit2.getText().toString(); 中获取值作为字符串,正如我之前所说,首先需要弄清楚输入的值是整数还是浮点数,
    【解决方案3】:

    我认为 EditText 中没有可用的 api 供开发人员获取值的类型。您可以在 JAVA 中找到另一种方法,apache 可能会提供一些小部件来处理此问题。

    【讨论】:

      【解决方案4】:

      在这种情况下,您可以使用字符串格式化程序。

      Formatting Numeric Print Output

      对于您的情况,您必须使用这样的模式。

      DecimalFormat df = new DecimalFormat("0.##"); 字符串 finalAnswer = df.format(answer);

      【讨论】:

        【解决方案5】:

        如果您的answer 是float,您可以在addition 函数中使用以下代码

        answer = numa + numb;
        String answerString = String.valueOf(answer);
        String decimalString = answerString.substring(answerString.indexOf(".") + 1);
        
        if (Integer.parseInt(decimalString) == 0)
            answerString = answerString.substring(0, answerString.indexOf("."));
        

        如果您的 answer 小数点后不是 0,这将返回 float 值,否则它将在 String 中返回 int

        【讨论】:

          【解决方案6】:

          用户总是输入String,没有使用editText输入其他类型但是,您需要检查editText是否有(。)因此它是float,然后将其解析为float,否则,它是Integer,然后将其解析为Integer .

          代码可能如下所示。

          if (num1.indexOf(".") != 0) {
              float numa = Float.parseFloat(num1);
              float numb = Float.parseFloat(num2);
          }
          else
          {
              int numa = Integer.parseInt(num1);
              int numb = Integer.parseInt(num2);
          }
          

          通过这样做,整数的输出将不会是分数样式

          附注: 您必须确保键盘只输入数字。因此您的应用在解析时不会崩溃。

          希望对你有帮助

          【讨论】:

            【解决方案7】:

            用户在 EditText 上输入的 Value 类型将始终为 String only。但是您可以通过 EditText 内的android:inputType="" 属性限制用户输入任何特定类型的值。

            【讨论】:

              猜你喜欢
              • 2012-09-19
              • 1970-01-01
              • 2021-12-03
              • 2020-03-25
              • 2020-03-31
              • 2011-03-17
              • 1970-01-01
              • 1970-01-01
              • 2011-05-30
              相关资源
              最近更新 更多