Aquila
Aquila

Reputation: 23

How can I remove the ".0" from a double? Android Studio

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

Upvotes: 0

Views: 6474

Answers (6)

itro
itro

Reputation: 7228

result = op1 + op2;
String endResult = (long) result  == result  ? "" + (long) result  : "" + result ; 
editText.setText("Result : " + endResult );

Upvotes: 0

stacky
stacky

Reputation: 820

The answer is here : https://stackoverflow.com/a/14126736/4017037

public static String fmt(double d)
{
    if(d == (int) d)
        return String.format("%d",(int)d);
    else
        return String.format("%s",d);
}

Upvotes: 2

Jimmar
Jimmar

Reputation: 4459

You can use the DecimalFormat that @Reimeus mentioned or do it this way [pure Java]

String outputString = Double.toString(result);
if(result == (int)result)
    outputString = Integer.toString((int)result);

Upvotes: 1

Madmenyo
Madmenyo

Reputation: 8584

Just cast it to a integer.

double d = 4.0;
int i = (int)d;
System.out.writeln((int)d)

However, this does not round your numbers properly if you have decimals from .5 and above. With Math.round() you can round decimals properly.

int i = Math.round(d);
System.out.writeln(Math.round(d));

This is slightly slower then just casting it to a int, but more precise.

If you need to check whether a double has decimal values higher then 0 then just subtract the cast integer from the double. If there is nothing left behind the decimal you can just output the integer.

Upvotes: 0

dvaey
dvaey

Reputation: 310

if(optr.equals("+")){
    op2 = Double.parseDouble(editText.getText().toString());
    editText.setText("");
    result = op1 + op2;
    editText.setText("Result : " + result);
}

Is one method. The string builder in setText will handle the double properly.

You can also use

editText.setText(String.format("Result: %f", result));

Which will parse the same as a printf expression.

Upvotes: 0

Reimeus
Reimeus

Reputation: 159864

You could use DecimalFormat to suppress the non-significant digits

NumberFormat format = new DecimalFormat("0.#");
editText.setText("Result : " + format.format(result));

Upvotes: 3

Related Questions