1

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

  1. how could i do this? ask the program to see whether the last digit is 0 and if yes, to replace by ""?
  2. 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!

Mat
  • 202,337
  • 40
  • 393
  • 406
pearmak
  • 4,979
  • 15
  • 64
  • 122

1 Answers1

2

The commas are easy to add:

public class DecimalFormatDemo {

static public void customFormat(String pattern, double value ) {
  DecimalFormat myFormatter = new DecimalFormat(pattern);
  String output = myFormatter.format(value);
  System.out.println(value + "  " + pattern + "  " + output);
}

static public void main(String[] args) {

  customFormat("###,###.###", 123456.789);
  customFormat("###.##", 123456.789);
  customFormat("000000.000", 123.78);
  customFormat("$###,###.###", 12345.67);  
}
}

The zeros go like this, I believe:

DecimalFormat myFormatter = new DecimalFormat("###,###,###,###.###");
String str1=Display.getText().toString();
String stripped1 = Double.valueOf(str1).toString();
stripped1 = myFormatter.format(Double.valueOf(stripped1));
if (stripped1.endsWith(".0"))
    stripped1 = stripped1.substring(0, stripped1.length() - 2);
Display.setText(stripped1);
dragostis
  • 2,574
  • 2
  • 20
  • 39
  • thanks!! sorry for being new to the android development...i have edited as above but 3+4 still = 7.0 instead of 7 – pearmak Aug 27 '12 at 15:32
  • if(Double.valueOf(stripped1) == (int) Double.valueOf(stripped1)) for the (int) Double.valueOf(stripped1) it say "cannot cast from Double to int"... – pearmak Aug 27 '12 at 16:23
  • wow...it works!! thanks so much!! Double.valueOf(str1).toString(); is to get rid of the extra 0? – pearmak Aug 27 '12 at 16:37
  • how to incorporate the above stripped1 together with the commas? – pearmak Aug 27 '12 at 17:15
  • sorry but this arise another problem...this is because the inputnum is processed as inputnum.add(Double.parseDouble(Display.getText().toString())); now the figures have "," so when first 1000+2000 and output 3,000, if further press the plus button, it will crash because there is a , inside 3000. So i added new string Displaytemp and newDisplaytemp as above code however it then say The method getText() is undefined for the type String and hence not suitable to be replaced as follows: inputnum.add(Double.parseDouble(newDisplaytemp.getText().toString())); – pearmak Aug 27 '12 at 18:06
  • Just keep the number(3000) into a variable and display the the parsed string separately, then when you want to add again, you add to the one you store previously(3000). – dragostis Aug 27 '12 at 18:10
  • Yes, it's a limitation of the Double type in Java. Aren't all your variables Doubles? – dragostis Aug 28 '12 at 12:57
  • yes they are doubles....as follows: if then how could this be improved? ArrayList inputnum = new ArrayList(); double inputnum1; double inputnum2; – pearmak Aug 28 '12 at 13:18
  • 1
    Then this is it. You cannot make them bigger. Every calculator that has big number will show 9.9e7 which means 9.9 * 10^7. If you feel you cannot live with this limitation, I suggest you revamp your calculator and use Strings instead of Double. The problem is that you will have to override every single operation; in other words you will have to teach the CPU to do basic sums, subtraction etc. – dragostis Aug 28 '12 at 13:21
  • that sounds troublesome...calculator is far more difficult to make that i expected...so how would it be different if change the variable to be float instead of double? – pearmak Aug 28 '12 at 14:58
  • It would be even worse. Still, you could make it better by storing the integer value of a double into an int an leaving only what's after comma in the double. Then, when you want to display it, concatenate the string from the int with the string from the double. – dragostis Aug 28 '12 at 15:07
  • actually just would like to know how that really operate...some even can plot graphs with lots of functions...would there be any good examples that can share? i have found this one at https://sourceforge.net/projects/androidcalculat/ but dont know how to open for that gz file...anyway many thanks for your kind help! – pearmak Aug 28 '12 at 15:21
  • That's a tar file for linux. Google it. – dragostis Aug 28 '12 at 15:25
  • how could the rounding eg 4000.84 - 4000 outputting as 0.840000000000146 instead of 0.84, be handled? – pearmak Aug 30 '12 at 18:24