0

I'm trying to do a calculator with the 4 basic operations. I started using doubles to get the arguments from edittext, but I discovered the problem with decimal values. To avoid that, I used BigDecimal, but now the app is failing at some specific numbers, as 1/(1.1). I noticed only the divide function is wrecking the app, add,sub and multiply are working fine. I really would appreciate some help with this. Here's part of the code:

 div.setOnClickListener( 
        new View.OnClickListener(){

                @Override
            public void onClick(View v){


                    if(! num1 .getEditableText().toString().matches("") && !num2 .getEditableText().toString().matches(""))
                    {String valor1 =num1.getText().toString();
                    String valor2 =num2.getText().toString();
                    BigDecimal a = new BigDecimal(valor1);
                    BigDecimal b = new BigDecimal(valor2);
                    BigDecimal result = a.divide(b);

                    Toast.makeText(MainActivity.this,"="+result, Toast.LENGTH_LONG).show();
                    }                                                                           }


            });
Community
  • 1
  • 1
rado
  • 5,720
  • 5
  • 29
  • 51
  • The app freezes and closes itself showing "Unfortunately, Calculator has stopped" It mainly happens with numbers that has 0.1 on it, like 50.1 , 2.1 etc – rado Dec 03 '13 at 12:50
  • 1
    From Docs: `divide(BigDecimal divisor)` Returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() - divisor.scale()); _if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown._ Try using other overloads – bansi Dec 03 '13 at 12:59

3 Answers3

1

It crashes on an ArithmeticException because the result is a number with infinite decimals.

From http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html

"In the case of divide, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3. If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown. Otherwise, the exact result of the division is returned, as done for other operations"

Ivo
  • 18,659
  • 2
  • 23
  • 35
1

If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown. Otherwise, the exact result of the division is returned, as done for other operations.

Use divide method like that

a.divide(b, 2, RoundingMode.HALF_UP)
where 2 is precision and RoundingMode.HALF_UP is rounding mode

source:https://stackoverflow.com/a/4591216/1589566

Community
  • 1
  • 1
mamba4ever
  • 2,662
  • 4
  • 28
  • 46
0

In your country it may be ',' and not '.'

String valor1 = num1.getText().toString().replace('.', ',');

Try this and tell us if it works !

Arthur Rey
  • 2,990
  • 3
  • 19
  • 42