I am now working on an android calculator and have some problems on displaying the result...the display is now set using string.format rounding at 9 decimal places...but though this is true for displaying endless decimal results, many other situations are not necessary, like
1+2 should display 3 instead of 3.000000000;
2.03*3 should display 6.09 instead of 6.090000000
- how could i do this? ask the program to see whether the last digit is 0 and if yes, to replace by ""?
- how could i display ",' for each thousand place? ie. 123,456,789 instead of solely 123456789 for both input and answer?
NEW PROBLEM ARISED!!
I newly discovered that after incorporating with the suggestions, for small figures manipulations like under 1000, the displayed answer is ok ( eg 999 * 999 probably displaying 998001). Yet If figures are over, the displayed answer become like this way, eg 9999 * 9999 = 9.998001e7. Is it related to the limitation of double? if then, how could it be solved?
the original coding is as follows:
case MULTIPLY:
inputnum1 = inputnum.get(0);
inputnum2 = inputnum.get(1);
inputnum.removeAll(inputnum);
inputnum.add(inputnum1 * inputnum2);
Display.setText(String.format("%.9f", inputnum.get(0)));
//
String str1=Display.getText().toString();
String stripped1 = Double.valueOf(str1).toString();
Display.setText(stripped1);
//
break;
the updated code as follows:
private void calculation (int operator) {
inputnum.add(Double.parseDouble(Display.getText().toString()));
if (operator != EQUALS) {nextOperation = operator;}
else if (operator == EQUALS){nextOperation = 0;}
switch (currentOperation) {
case MULTIPLY:
inputnum1 = inputnum.get(0);
inputnum2 = inputnum.get(1);
inputnum.removeAll(inputnum);
inputnum.add(inputnum1 * inputnum2);
Display.setText(String.format("%.19f", inputnum.get(0)));
DecimalFormat myFormatter3 = new DecimalFormat("###,###,###,###,###,###.#########");
String str3=Display.getText().toString();
String stripped3 = Double.valueOf(str3).toString();
stripped3 = myFormatter3.format(Double.valueOf(stripped3));
if (stripped3.endsWith(".0"))
stripped3 = stripped3.substring(0, stripped3.length() - 2);
Display.setText(stripped3);
similar for case SUBTRACT: case ADD: case DIVISION:
Many thanks!