Question

I'm creating a calculator for Android, Well so far it looks pretty much done... The thing is the answers are not precise, Addition, subtraction, multiplication work fine as decimal is usually not required.. When it comes to division the answer is rounded to the nearest integer rather than showing the answer in decimal... So i changed the TextView type to show decimals and used float instead of int for the variable containing the answer. The result is I end up with a rounded integer with a ".00" in the ending :/ Please help! My code : MainActivity.java :

package in.rohanbojja.basiccalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void add( View view){

        EditText n1 = (EditText) findViewById(R.id.editText1);
        EditText n2 = (EditText) findViewById(R.id.editText3);
        TextView res = (TextView) findViewById(R.id.textView1);

        String sn1 = n1.getText().toString();
        String sn2 = n2.getText().toString();
        String sres;

        int in1 = Integer.parseInt(sn1);
        int in2 = Integer.parseInt(sn2);
        float ires;

        ires = in1 + in2;
        sres = Float.toString(ires);
        res.setText(sres);
}
public void sub( View view ){

    EditText n1 = (EditText) findViewById(R.id.editText1);
    EditText n2 = (EditText) findViewById(R.id.editText3);
    TextView res = (TextView) findViewById(R.id.textView1);

    String sn1 = n1.getText().toString();
    String sn2 = n2.getText().toString();
    String sres;

    int in1 = Integer.parseInt(sn1);
    int in2 = Integer.parseInt(sn2);
    float ires;

    ires = in1 - in2;
    sres = Float.toString(ires);
    res.setText(sres);
}
public void mul( View view ){

    EditText n1 = (EditText) findViewById(R.id.editText1);
    EditText n2 = (EditText) findViewById(R.id.editText3);
    TextView res = (TextView) findViewById(R.id.textView1);

    String sn1 = n1.getText().toString();
    String sn2 = n2.getText().toString();
    String sres;

    int in1 = Integer.parseInt(sn1);
    int in2 = Integer.parseInt(sn2);
    float ires;

    ires = in1 * in2;
    sres = Float.toString(ires);
    res.setText(sres);

}


public void clr( View view ){

    EditText n1 = (EditText) findViewById(R.id.editText1);
    EditText n2 = (EditText) findViewById(R.id.editText3);
    TextView res = (TextView) findViewById(R.id.textView1);

    n1.setText("");
    n2.setText("");
    res.setText("Result");
}

public void div( View view){
    EditText n1 = (EditText) findViewById(R.id.editText1);
    EditText n2 = (EditText) findViewById(R.id.editText3);
    TextView res = (TextView) findViewById(R.id.textView1);

    String sn1 = n1.getText().toString();
    String sn2 = n2.getText().toString();
    String sres;

    int in1 = Integer.parseInt(sn1);
    int in2 = Integer.parseInt(sn2);
    float ires;

    ires = in1/in2;
    sres = Float.toString(ires);
    res.setText(sres);
}
}
Was it helpful?

Solution 2

I see that you have tried to force a floating-point result by storing the result in a float, but unfortunately this won't do it. Operators in Java, such as the division operator (/) work like this:

  • If BOTH operands are floats, the result is a float.
  • If ONE of the operands is a float, the result is a float.
  • If NEITHER of the operands are floats (i.e. they are both integers), the result is an int.

Since yours falls into the latter case, you get an integer result. The solution is to force one of your operands to be a float, not just the resulting variable, like so:

ires = ((float) in1) / in2;

OTHER TIPS

int in1 = Integer.parseInt(sn1);
int in2 = Integer.parseInt(sn2);
float ires;

ires = in1/in2;

You are still doing integer division, you're just storing the result in a float. To perform floating point division, in1 and in2 must be floats.

In div : in1 and in2 are ints. You need to parse sn1 and sn2 as Floats.

your have some little mistake, you should conver in1 and in2 to float, or something like this:

ires = (float)in1/in2;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top