-2

In this code text view for every integer number and decimal number if i give as an input in text box in text view its showing with .0 how to remove that

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


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

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

      }
    }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
JURAIJ
  • 11
  • 1
  • 2

2 Answers2

3

If you don't need the decimal places, just use

Integer.parseInt()

in your code.

Otherwise, you can use String.format() to format the string to display 0 decimal places:

String.format("%.0d", yourDouble)
FD_
  • 12,947
  • 4
  • 35
  • 62
  • +1 Thanks @FD_ that's a useful regex for me, despite the... long winded, shoutey question asked, your answer is helpful, thanks! – RossC Feb 21 '14 at 11:44
  • 1
    @RossC For reference, that's a [format string](http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax), not a regex :p – thegrinner Feb 21 '14 at 13:42
  • @thegrinner Thanks for that, I haven't seen it before, much appreciated! – RossC Feb 21 '14 at 14:20
0

Simply Replace your last lines

String text=String.valueOf(result);
String str[]= text.split(".");
tvResult.setText(num1 + " " + oper + " " + num2 + " = " + str[1]);

with

tvResult.setText(num1 + " " + oper + " " + num2 + " = " + Integer.parseInt(result));
Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28