android计算器显示-截断不必要的零,并为显示输入和答案添加分隔符

android计算器显示-截断不必要的零,并为显示输入和答案添加分隔符,android,calculator,Android,Calculator,我现在正在使用android计算器,在显示结果时遇到了一些问题…现在使用string.format四舍五入将显示设置为小数点后9位…但尽管这对于显示无休止的小数点结果是正确的,但许多其他情况都是不必要的,例如 1+2应显示3,而不是3.000000000 2.03*3应显示6.09而不是6.090000000 我怎么能这样做?要求程序查看最后一位数字是否为0,如果是,则替换为“” 我如何才能为每千个位置显示“,”?即123456789,而不是仅为输入和回答显示123456789 新问题出现了!!

我现在正在使用android计算器,在显示结果时遇到了一些问题…现在使用string.format四舍五入将显示设置为小数点后9位…但尽管这对于显示无休止的小数点结果是正确的,但许多其他情况都是不必要的,例如

1+2应显示3,而不是3.000000000

2.03*3应显示6.09而不是6.090000000

  • 我怎么能这样做?要求程序查看最后一位数字是否为0,如果是,则替换为“”
  • 我如何才能为每千个位置显示“,”?即123456789,而不是仅为输入和回答显示123456789
  • 新问题出现了!! 我最近发现,在加入这些建议后,对于1000以下的小数字操作,显示的答案是可以的(例如999*999可能显示998001)但如果数字结束,显示的答案会变成这样,例如9999*9999=9.998001e7。这是否与双精度限制有关?如果是,如何解决

    原始编码如下:

        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;
    
        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);
    
    更新代码如下:

        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;
    
        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);
    
    类似于 案例减去: 案例补充: 案件司:


    非常感谢!

    逗号很容易添加:

    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);  
    }
    }
    
    我相信零是这样的:

    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);
    

    看到感谢!!很抱歉我是android开发的新手…我如上所述进行了编辑,但3+4仍然=7.0,而不是(int)Double的7if(Double.valueOf(stripped1)==(int)Double.valueOf(stripped1))。它说“不能从Double转换为int”…哇…它工作了!!非常感谢!!Double.valueOf(str1)。toString();是要去掉多余的0吗?如何将上面的带条纹的1与逗号合并在一起?那么就是这样了。你不能把它们变大。每个有大数字的计算器都会显示9.9e7,这意味着9.9*10^7。如果你觉得你不能接受这个限制,我建议你改进你的计算器,使用字符串而不是双精度。问题em是你必须重写每一个操作;换句话说,你必须教CPU做基本的求和、减法等。