0

For my simple calculator, I am showing the result in a TextView, but it is always showing decimals. How can I remove them ?

This is my code :

public class MainActivity extends Activity implements OnClickListener {

  EditText etNum1;
  EditText etNum2;

  Button btnAdd;
  Button btnSub;
  Button btnMult;
  Button btnDiv;

  TextView tvResult;

  String oper = "";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // find the elements
    etNum1 = (EditText) findViewById(R.id.etNum1);
    etNum2 = (EditText) findViewById(R.id.etNum2);

    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnSub = (Button) findViewById(R.id.btnSub);
    btnMult = (Button) findViewById(R.id.btnMult);
    btnDiv = (Button) findViewById(R.id.btnDiv);

    tvResult = (TextView) findViewById(R.id.tvResult);

    // set a listener
    btnAdd.setOnClickListener((OnClickListener) this);
    btnSub.setOnClickListener(this);
    btnMult.setOnClickListener(this);
    btnDiv.setOnClickListener(this);

  }
      @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
     double  num1=0;
     double num2=0;
     double result=0;


    // check if the fields are empty
    if (TextUtils.isEmpty(etNum1.getText().toString())
        || TextUtils.isEmpty(etNum2.getText().toString())) {
     return;
    }

    // read EditText and fill variables with numbers

    num1 = Double.parseDouble(etNum1.getText().toString());
   num2 = Double.parseDouble(etNum2.getText().toString());



    // defines the button that has been clicked and performs the corresponding operation
    // write operation into oper, we will use it later for output
    switch (v.getId()) {
    case R.id.btnAdd:
      oper = "+";

     result = num1 + num2;

      break;
    case R.id.btnSub:
      oper = "-";
      result = num1 - num2;

      break;
    case R.id.btnMult:
      oper = "*";
      result = num1 * num2;

      break;
    case R.id.btnDiv:
      oper = "/";
     result = num1 / num2;

      break;
    default:
      break;
    }

    // form the output line




    tvResult.setText(num1 + " " + oper + " " + num2 + " = " + result);

  }
}
2Dee
  • 8,609
  • 7
  • 42
  • 53
JURAIJ
  • 11
  • 1
  • 2

3 Answers3

1

A non mathematical way to do it:

A short and simple method is, convert the double to string:

String text = String.valueOf(result);

Now you have the result in the string. Given that your requirement is that you don't want decimals, so split your string based on "." as a delimiter:

String str[] = text.split(".");

Now in str[0] you will have only the number part. so set it to the text view:

tvResult.setText(num1 + " " + oper + " " + num2 + " = " + str[0]);

I'm sure this one works fine.

clstrfsck
  • 14,715
  • 4
  • 44
  • 59
Zax
  • 2,870
  • 7
  • 52
  • 76
0

You can use DecimalFormat to format the result:

DecimalFormat df = new DecimalFormat("0.##########");
tvResult.setText(num1 + " " + oper + " " + num2 + " = " + df.format(result));

This will print with up to 10 decimal places.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
0

Many ways to do that, i use:

public static double round(double value, int places) {
        if (places < 0)
            throw new IllegalArgumentException();

        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(places, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }  

And then call:

round(yourAnswer, 2)  

For BigDecimal:
http://developer.android.com/reference/java/math/BigDecimal.html
Efficient BigDecimal round Up and down to two decimals

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51