1
I have this code below that is a calculator, but I have a problem: I wanted to return a value float
when dividing 5 / 2 = 2.5 and returning an integer value when dividing 4 / 2 = 2 and not 2.0!
Can anyone help me on that? The code is this:
public class CalcActivity extends Activity
{
TextView tvScreenCalc;
String currentString="0",previusString=null;
boolean isTempStringShown=false;
int currentopperand=0;
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calc);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT);
tvScreenCalc=(TextView)findViewById(R.id.tvScreenCalc);
int numberButtons[]={R.id.button0,R.id.button1,R.id.button2,R.id.button3,R.id.button4,R.id.button5,R.id.button6,R.id.button7,R.id.button8,R.id.button9};
NumberButtonClickListener numberClickListener=new NumberButtonClickListener();
for(int id:numberButtons)
{ View v=findViewById(id);
v.setOnClickListener(numberClickListener);
}
int opperandButtons[]={R.id.buttonPlus,R.id.buttonMinus,R.id.buttonDivide,R.id.buttonTimes,R.id.buttonDecimal,R.id.buttonClear,R.id.buttonEquals};
OpperandButtonClickListener oppClickListener=new OpperandButtonClickListener();
for(int id:opperandButtons)
{ View v=findViewById(id);
v.setOnClickListener(oppClickListener);
}
setCurrentString("0");
}
void setCurrentString(String s)
{ currentString=s;
tvScreenCalc.setText(s);
}
class NumberButtonClickListener implements OnClickListener
{ @Override public void onClick(View v)
{ if(isTempStringShown)
{ previusString=currentString;
currentString="0";
isTempStringShown=false;
}
String text=(String)((Button)v).getText();
if(currentString.equals("0"))setCurrentString(text);
else setCurrentString(currentString+text);
}
}
class OpperandButtonClickListener implements OnClickListener
{ @Override public void onClick(View v)
{ int id=v.getId();
if(id==R.id.buttonClear)
{ isTempStringShown=false;
setCurrentString("0");
previusString=null;
}
if(id==R.id.buttonDecimal)if(!currentString.contains("."))setCurrentString(currentString+".");
if(id==R.id.buttonPlus||id==R.id.buttonMinus||id==R.id.buttonTimes||id==R.id.buttonDivide)
{ currentopperand=id;
previusString=currentString;
isTempStringShown=true;
}
if(id==R.id.buttonEquals)
{ double curr=Double.parseDouble(currentString);
double result=0;
if(previusString!=null)
{ double prev=Double.parseDouble(previusString);
switch(currentopperand)
{ case R.id.buttonPlus: result=prev+curr; break;
case R.id.buttonMinus: result=prev-curr; break;
case R.id.buttonTimes: result=prev*curr; break;
case R.id.buttonDivide: result=prev/curr; break;
}
}
setCurrentString(Double.toString(result));
}
}
}
}
Only use some formatter which, when it is only zero after comma, remove decimal place, do not need to change type
– user28595
And how do I do that? I’m new to programming, and in the research I do, there’s never what I want!
– Dennis Wadson
If you don’t see what you want in the searches you do, it’s because you’re not doing the right searches! : D. It seems that it is not yet necessary to have some knowledge to know what to look for. As a small aside, start by indenting the code which has much more impact and positive effects than you might think. Regarding the problem itself, you can see if the result of the division is equal to the same result converted to
(int)
, This allows you to know whether you should have decimals or not.– Isac