I'm making a calculator for Android but I have a problem with the double. When I write a operation like 2+2 the result is 4.0 instead of 4 . How can I remove the ".0" from the result? This is my Activity:
Button button0, button1, button2, button3, button4, button5, button6 , button7, button8, button9,
buttonClear, buttonDelete, buttonEqual, buttonMin, buttonPar, buttonPlus, buttonX, buttonRad,
buttonSlash, buttonVirg;
EditText editText;
double op1;
double op2;
double result;
String optr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rect_activity_my);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
editText = (EditText) findViewById(R.id.editText);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonClear = (Button) findViewById(R.id.buttonClear);
buttonEqual = (Button) findViewById(R.id.buttonEqual);
buttonSlash = (Button) findViewById(R.id.buttonSlash);
buttonDelete = (Button) findViewById(R.id.buttonDelete);
buttonX = (Button) findViewById(R.id.buttonX);
buttonMin = (Button) findViewById(R.id.buttonMin);
buttonPlus = (Button) findViewById(R.id.buttonPlus);
buttonDot = (Button) findViewById(R.id.buttonDot);
try{
button0.setOnClickListener(this);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
buttonClear.setOnClickListener(this);
buttonDelete.setOnClickListener(this);
buttonEqual.setOnClickListener(this);
buttonMin.setOnClickListener(this);
buttonPlus.setOnClickListener(this);
buttonX.setOnClickListener(this);
buttonSlash.setOnClickListener(this);
buttonDot.setOnClickListener(this);
}
catch(Exception e){
}
}
public void operation(){
if(optr.equals("+")){
op2 = Double.parseDouble(editText.getText().toString());
editText.setText("");
result = op1 + op2;
editText.setText("Result : " + Double.toString(result));
}
else if(optr.equals("-")){
op2 = Double.parseDouble(editText.getText().toString());
editText.setText("");
result = op1 - op2;
editText.setText("Result : " + Double.toString(result));
}
else if(optr.equals("*")){
op2 = Double.parseDouble(editText.getText().toString());
editText.setText("");
result = op1 * op2;
editText.setText("Result : " + Double.toString(result));
}
else if(optr.equals("/")){
op2 = Double.parseDouble(editText.getText().toString());
editText.setText("");
result = op1 / op2;
editText.setText("Result : " + Double.toString(result));
}
}
@Override
public void onClick(View arg0) {
Editable str = editText.getText();
switch(arg0.getId()){
case R.id.button1:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button1.getText());
editText.setText(str);
break;
case R.id.button2:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button2.getText());
editText.setText(str);
break;
case R.id.button3:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button3.getText());
editText.setText(str);
break;
case R.id.button4:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button4.getText());
editText.setText(str);
break;
case R.id.button5:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button5.getText());
editText.setText(str);
break;
case R.id.button6:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button6.getText());
editText.setText(str);
break;
case R.id.button7:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button7.getText());
editText.setText(str);
break;
case R.id.button8:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button8.getText());
editText.setText(str);
break;
case R.id.button9:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button9.getText());
editText.setText(str);
break;
case R.id.button0:
if(op2 != 0){
op2 = 0;
editText.setText("");
}
str = str.append(button0.getText());
editText.setText(str);
break;
case R.id.buttonClear:
op1 = 0;
op2 = 0;
editText.setText("");
break;
case R.id.buttonDot:
if (op1 == 0){
op1 = Double.parseDouble(editText.getText().toString());
editText.setText(op1 + ".");
} else if (op2 == 0){
op2 = Double.parseDouble(editText.getText().toString());
editText.setText(op2 + ".");
}
break;
case R.id.buttonDelete:
editText.setText(str);
if (str.length() > 1){
str = (Editable) str.subSequence(0, str.length() - 1);
editText.setText(str);
}else if (str.length() <= 1){
editText.setText("");
}
break;
case R.id.buttonPlus:
optr = "+";
if(op1 == 0){
op1 = Double.parseDouble(editText.getText().toString());
editText.setText("");
}
else if(op2 != 0){
op2 = 0;
editText.setText("");
}
else{
op2 = Double.parseDouble(editText.getText().toString());
editText.setText("");
result = op1 + op2;
editText.setText("Result : " + Double.toString(result));
}
break;
case R.id.buttonMin:
optr = "-";
if(op1 == 0){
op1 = Double.parseDouble(editText.getText().toString());
editText.setText("");
}
else if(op2 != 0){
op2 = 0;
editText.setText("");
}
else{
op2 = Double.parseDouble(editText.getText().toString());
editText.setText("");
result = op1 - op2;
editText.setText("Result : " + Double.toString(result));
}
break;
case R.id.buttonX:
optr = "*";
if(op1 == 0){
op1 = Double.parseDouble(editText.getText().toString());
editText.setText("");
}
else if(op2 != 0){
op2 = 0;
editText.setText("");
}
else{
op2 = Double.parseDouble(editText.getText().toString());
editText.setText("");
result = op1 * op2;
editText.setText("Result : " + Double.toString(result));
}
break;
case R.id.buttonSlash:
optr = "/";
if(op1 == 0){
op1 = Double.parseDouble(editText.getText().toString());
editText.setText("");
}
else if(op2 != 0){
op2 = 0;
editText.setText("");
}
else{
op2 = Double.parseDouble(editText.getText().toString());
editText.setText("");
result = op1 / op2;
editText.setText("Result : " + Double.toString(result));
}
break;
case R.id.buttonEqual:
if(!optr.equals(null)){
if(op2 != 0){
if(optr.equals("+")){
editText.setText("");
/*op1 = op1 + op2;*/
editText.setText("Result : " + Double.toString(result));
}
else if(optr.equals("-")){
editText.setText("");/*
op1 = op1 - op2;*/
editText.setText("Result : " + Double.toString(result));
}
else if(optr.equals("*")){
editText.setText("");/*
op1 = op1 * op2;*/
editText.setText("Result : " + Double.toString(result));
}
else if(optr.equals("/")){
editText.setText("");/*
op1 = op1 / op2;*/
editText.setText("Result : " + Double.toString(result));
}
}
else{
operation();
}
}
break;
}
}
}
Thanks in advance