android - What is the bigger datatype than double in Java? -
i developing android app, calculator. have used double save operands. calculator calculates bigger number, it's not showing answer properly. example, if enters 1234567890 * 1, shows me result 1234567936. think due range issues of double. how solve it? help!
this part of actual code containing operator's functioning:
badd.setonclicklistener(new view.onclicklistener() { // add button // listener @override public void onclick(view v) { // todo auto-generated method stub try { if (switch == true) { display.settext(display.gettext() + ""); op1 += float.valueof(display.gettext().tostring()); check = 'a'; display.settext(" "); display2.settext("+"); } else if (display.gettext().tostring() != " " && switch == false) { display.settext(display.gettext() + ""); op1 = float.valueof(display.gettext().tostring()); check = 'a'; switch = true; display.settext(" "); display2.settext("+"); } else { throw new exception(); } } catch (exception e) { display.settext(" "); } } }); bsub.setonclicklistener(new view.onclicklistener() { // subtract // button // listener @override public void onclick(view v) { // todo auto-generated method stub try { if (switch == true) { display.settext(display.gettext() + ""); op1 -= float.valueof(display.gettext().tostring()); check = 's'; display.settext(" "); display2.settext("-"); } else if (display.gettext().tostring() != " " && switch == false) { display.settext(display.gettext() + ""); op1 = float.valueof(display.gettext().tostring()); check = 's'; switch = true; display.settext(" "); display2.settext("-"); } else { throw new exception(); } } catch (exception e) { display.settext(" "); } } }); bmul.setonclicklistener(new view.onclicklistener() { // multiply // button // listener @override public void onclick(view v) { // todo auto-generated method stub try { if (switch == true) { display.settext(display.gettext() + ""); op1 *= float.valueof(display.gettext().tostring()); check = 'm'; display.settext(" "); display2.settext("x"); } else if (display.gettext().tostring() != " " && switch == false) { display.settext(display.gettext() + ""); op1 = float.valueof(display.gettext().tostring()); check = 'm'; switch = true; display.settext(" "); display2.settext("x"); } else { throw new exception(); } } catch (exception e) { display.settext(" "); } } }); bdiv.setonclicklistener(new view.onclicklistener() { // divide // button // listener @override public void onclick(view v) { // todo auto-generated method stub try { if (switch == true) { display.settext(display.gettext() + ""); op1 /= float.valueof(display.gettext().tostring()); check = 'd'; display.settext(" "); display2.settext("/"); } else if (display.gettext().tostring() != " " && switch == false) { display.settext(display.gettext() + ""); op1 = float.valueof(display.gettext().tostring()); check = 'd'; switch = true; display.settext(" "); display2.settext("/"); } else { throw new exception(); } } catch (exception e) { display.settext(" "); } } });
this part of actual code containing equal's functioning:
beq.setonclicklistener(new view.onclicklistener() { // equals // button // listener @override public void onclick(view v) { // todo auto-generated method stub try { if (display.gettext().tostring() != " ") { double result; int temp = 0; op2 = float.valueof(display.gettext().tostring()); op1 = math.floor(op1 * 100) / 100; op2 = math.floor(op2 * 100) / 100; if (check == 'a') { result = op1 + op2; temp = (int) result; if (result - temp == 0) { display.settext(string.valueof(temp)); } else { display.settext(string.valueof(result)); } } else if (check == 's') { result = op1 - op2; temp = (int) result; if (result - temp == 0) { display.settext(string.valueof(temp)); } else { display.settext(string.valueof(result)); } } else if (check == 'm') { result = op1 * op2; temp = (int) result; if (result - temp == 0) { display.settext(string.valueof(temp)); } else { display.settext(string.valueof(result)); } } else if (check == 'd') { if (op2 != 0) { result = op1 / op2; temp = (int) result; if (result - temp == 0) { display.settext(string.valueof(temp)); } else { display.settext(string.valueof(result)); } } else { display.settext(" "); display2.settext("invalid"); } } else if (check == 'p') { try { if (op2 == 0) { throw new exception(); } else { result = (op1 / op2) * 100; temp = (int) result; if (result - temp == 0) { display.settext(string .valueof(temp) + "%"); } else { display.settext(string .valueof(result) + "%"); } } } catch (exception e) { display.settext(" "); display2.settext("invalid"); } } } else { display.settext(" "); } switch = false; check = '\0'; display2.settext("="); } catch (exception e) { display.settext(" "); } } }); } { } }
you use biginteger
or bigdecimal
if need big numbers. 1 gave in example, though, should fit humble int
, there's wrong code.
Comments
Post a Comment