Android: Calculator on showing 0 immediately after the dot

Posted by pearmak on Stack Overflow See other posts from Stack Overflow or by pearmak
Published on 2012-10-27T04:56:09Z Indexed on 2012/10/27 5:00 UTC
Read the original article Hit count: 150

Filed under:
|
|

I am now working on a calculator, and everything works fine except for decimal places.

The calculator contains 2 displays actually, one is called fakedisplay for actual operations, and one is called Display, for presenting the desired format, ie adding commas.

When pressing 12345.678, Display will follow fakedisplay and present as 12,345.678, but if i press 12345.009, the fakedisplay will work normally as 12345.009, but the Display stuck as 12,345 until 9 is pressed, and at that time it will show 12,345.009 normally.

However, it is strange that when the user presses 0, there is no response, and until pressing 9, 009 will then immediately append.

I know this arise from the parsing code, but based on this, how could I amend the following code? I really cannot think of any solution... Many thanks for all your advice!

    one.setOnClickListener(new View.OnClickListener() {
            if (str.length()<15) {Fakedisplay.append("1");}
    DecimalFormat myFormatter1 = new DecimalFormat("###,###,###,###.#################"); 
    String str1=Fakedisplay.getText().toString(); 
    String stripped1 = Double.valueOf(str1).toString(); 
    stripped1 = myFormatter1.format(Double.valueOf(stripped1)); 
    if (stripped1.endsWith(".0")) 
        stripped1 = stripped1.substring(0, stripped1.length() - 2); 
    Display.setText(stripped1);

© Stack Overflow or respective owner

Related posts about android

Related posts about decimal