java - Multiplication in Calculator Android App throwing alphabeta in output -
below code calculator android app making:
package com.example.calculator; import java.text.decimalformat; import java.text.numberformat; import java.util.iterator; import java.util.stack; import java.lang.math; import android.app.activity; import android.os.bundle; import android.widget.adapterview; import android.widget.button; import android.widget.textview; import android.widget.adapterview.onitemclicklistener; import android.widget.gridview; import android.view.view; import android.view.view.onclicklistener; public class main extends activity { gridview mkeypadgrid; textview userinputtext; textview memorystattext; stack<string> minputstack; stack<string> moperationstack; keypadadapter mkeypadadapter; textview mstacktext; boolean resetinput = false; boolean hasfinalresult = false; string mdecimalseperator; double memoryvalue = double.nan; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); decimalformat currencyformatter = (decimalformat) numberformat .getinstance(); char decimalseperator = currencyformatter.getdecimalformatsymbols() .getdecimalseparator(); mdecimalseperator = character.tostring(decimalseperator); setcontentview(r.layout.main); // create stack minputstack = new stack<string>(); moperationstack = new stack<string>(); // reference keypad button gridview mkeypadgrid = (gridview) findviewbyid(r.id.grdbuttons); // reference user input textview userinputtext = (textview) findviewbyid(r.id.txtinput); userinputtext.settext("0"); memorystattext = (textview) findviewbyid(r.id.txtmemory); memorystattext.settext(""); mstacktext = (textview) findviewbyid(r.id.txtstack); // create keypad adapter mkeypadadapter = new keypadadapter(this); // set adapter of keypad grid mkeypadgrid.setadapter(mkeypadadapter); // set button click listener of keypad adapter mkeypadadapter.setonbuttonclicklistener(new onclicklistener() { @override public void onclick(view v) { button btn = (button) v; // keypadbutton value used identify // keypad button button's tag keypadbutton keypadbutton = (keypadbutton) btn.gettag(); // process keypad button processkeypadinput(keypadbutton); } }); mkeypadgrid.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { } }); } private void processkeypadinput(keypadbutton keypadbutton) { //toast.maketext(this, keypadbutton.gettext(), toast.length_short).show(); string text = keypadbutton.gettext().tostring(); string currentinput = userinputtext.gettext().tostring(); int currentinputlen = currentinput.length(); string evalresult = null; double userinputvalue = double.nan; switch (keypadbutton) { case backspace: // handle backspace // if has operand skip backspace if (resetinput) return; int endindex = currentinputlen - 1; // there 1 character @ input reset input 0 if (endindex < 1) { userinputtext.settext("0"); } // trim last character of input text else { userinputtext.settext(currentinput.subsequence(0, endindex)); } break; case sign: // handle -/+ sign // input has text , different initial value 0 if (currentinputlen > 0 && currentinput != "0") { // has (-) sign. remove sign if (currentinput.charat(0) == '-') { userinputtext.settext(currentinput.subsequence(1, currentinputlen)); } // prepend (-) sign else { userinputtext.settext("-" + currentinput.tostring()); } } break; case ce: // handle clear input userinputtext.settext("0"); break; case c: // handle clear input , stack userinputtext.settext("0"); clearstacks(); break; case decimal_sep: // handle decimal separator if (hasfinalresult || resetinput) { userinputtext.settext("0" + mdecimalseperator); hasfinalresult = false; resetinput = false; } else if (currentinput.contains(".")) return; else userinputtext.append(mdecimalseperator); break; case div: case plus: case minus: case multiply: if (resetinput) { minputstack.pop(); moperationstack.pop(); } else { if (currentinput.charat(0) == '-') { minputstack.add("(" + currentinput + ")"); } else { minputstack.add(currentinput); } moperationstack.add(currentinput); } minputstack.add(text); moperationstack.add(text); dumpinputstack(); evalresult = evaluateresult(false); if (evalresult != null) userinputtext.settext(evalresult); resetinput = true; break; case calculate: if (moperationstack.size() == 0) break; moperationstack.add(currentinput); evalresult = evaluateresult(true); if (evalresult != null) { clearstacks(); userinputtext.settext(evalresult); resetinput = false; hasfinalresult = true; } break; case m_add: // add user input value memory buffer userinputvalue = tryparseuserinput(); if (double.isnan(userinputvalue)) return; if (double.isnan(memoryvalue)) memoryvalue = 0; memoryvalue += userinputvalue; displaymemorystat(); hasfinalresult = true; break; case m_remove: // subtract user input value memory buffer userinputvalue = tryparseuserinput(); if (double.isnan(userinputvalue)) return; if (double.isnan(memoryvalue)) memoryvalue = 0; memoryvalue -= userinputvalue; displaymemorystat(); hasfinalresult = true; break; case mc: // reset memory buffer 0 memoryvalue = double.nan; displaymemorystat(); break; case mr: // read memorybuffer value if (double.isnan(memoryvalue)) return; userinputtext.settext(doubletostring(memoryvalue)); displaymemorystat(); break; case ms: // set memorybuffer value user input userinputvalue = tryparseuserinput(); if (double.isnan(userinputvalue)) return; memoryvalue = userinputvalue; displaymemorystat(); hasfinalresult = true; break; default: if (character.isdigit(text.charat(0))) { if (currentinput.equals("0") || resetinput || hasfinalresult) { userinputtext.settext(text); resetinput = false; hasfinalresult = false; } else { userinputtext.append(text); resetinput = false; } } break; } } private void clearstacks() { minputstack.clear(); moperationstack.clear(); mstacktext.settext(""); } private void dumpinputstack() { iterator<string> = minputstack.iterator(); stringbuilder sb = new stringbuilder(); while (it.hasnext()) { charsequence ivalue = it.next(); sb.append(ivalue); } mstacktext.settext(sb.tostring()); } private string evaluateresult(boolean requestedbyuser) { if ((!requestedbyuser && moperationstack.size() != 4) || (requestedbyuser && moperationstack.size() != 3)) return null; string left = moperationstack.get(0); string operator = moperationstack.get(1); string right = moperationstack.get(2); string tmp = null; if (!requestedbyuser) tmp = moperationstack.get(3); double leftval = double.parsedouble(left.tostring()); double rightval = double.parsedouble(right.tostring()); double result = double.nan; if (operator.equals(keypadbutton.div.gettext())) { result = leftval / rightval; } else if (operator.equals(keypadbutton.multiply.gettext())) { result = leftval * rightval; } else if (operator.equals(keypadbutton.plus.gettext())) { result = leftval + rightval; } else if (operator.equals(keypadbutton.minus.gettext())) { result = leftval - rightval; } string resultstr = doubletostring(result); if (resultstr == null) return null; moperationstack.clear(); if (!requestedbyuser) { moperationstack.add(resultstr); moperationstack.add(tmp); } return resultstr; } private string doubletostring(double value) { if (double.isnan(value)) return null; long longval = (long) value; if (longval == value) return long.tostring(longval); else return double.tostring(value); } private double tryparseuserinput() { string inputstr = userinputtext.gettext().tostring(); double result = double.nan; try { result = double.parsedouble(inputstr); } catch (numberformatexception nfe) { } return result; } private void displaymemorystat() { if (double.isnan(memoryvalue)) { memorystattext.settext(""); } else { memorystattext.settext("m = " + doubletostring(memoryvalue)); } } }
when give inputs 45454545454545 * 54545454545454, output shown 2.479338842975157e27. alphabet e in output.
the multiplication code is:
if (operator.equals(keypadbutton.multiply.gettext())) { result = leftval * rightval; }
instead of e, want show result _______ * 10^x. can me how fix this? in advance.
you're talking how display numbers.. can use numberformat
specify how number should displayed, e.g.:
numberformat formatter = new decimalformat("#0.00");
for more info:
- http://developer.android.com/reference/java/text/numberformat.html
- http://developer.android.com/reference/java/text/decimalformat.html
here example: http://www.coderzheaven.com/2012/10/14/numberformat-class-android-rounding-number-android-formatting-decimal-values-android/
if want keep , want replace exponential e
^
, why not try:
string.replace("e", "^")
Comments
Post a Comment