java - android EditText calculating wrong result -
i trying make simple app converts between celsius , fahrenheit:
i wrote 2 edittext
, 1 input , output, have 2 buttons (toc
, tof
).
the code following:
public class mainactivity extends actionbaractivity { private edittext edittext_input; private edittext edittext_output; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); edittext_input = (edittext) findviewbyid(r.id.input); edittext_output = (edittext) findviewbyid(r.id.output); button toc = (button) findviewbyid(r.id.c); toc.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string inputs = edittext_input.gettext().tostring(); double inputd = double.parsedouble(inputs); double outputd = (5/9) * (inputd-32); string outputs = string.valueof(outputd); //toast.maketext(mainactivity.this,string.valueof(outputd),toast.length_long).show(); edittext_output.settext(outputs, textview.buffertype.editable); } }); button tof = (button) findviewbyid(r.id.f); tof.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { string inputs = edittext_input.gettext().tostring(); double inputd = double.parsedouble(inputs); double outputd = (9/5) * inputd + 32; string outputs = string.valueof(outputd); edittext_output.settext(outputs, textview.buffertype.editable); } }); }
after clicking button toc
, produces 0.0, , clicking button tof
produce wrong result. set inputtype
edittext
numerdecimal
, tried number still doesn't work.
i checked problem outputd
, have no idea why outputd
can't correct result.
could me?
when calculate double outputd = (5/9) * (inputd-32);
compiler assumes 5
, 9
int
s, 5/9
zero. if want compiler treat numbers doubles
, should write double outputd = (5.0/9.0) * (inputd-32);
. same applies second conversion - double outputd = (9/5) * inputd + 32;
Comments
Post a Comment