【问题标题】:Output string with decimal format limited to 2 dp十进制格式的输出字符串限制为 2 dp
【发布时间】:2013-01-29 19:01:55
【问题描述】:

坚持这一点,我确信有一个简单的解决方案,只是无法解决!

我尝试过十进制格式、数字格式、字符串格式()等,但没有任何效果。 .

下面的代码,我想计算只显示限制为 2 位小数的输出。在过去的 2 个小时里尝试了各种方法,所有这些方法都会导致应用在运行时崩溃……

    Output = (Output1 / (1 -(Output2/100)))

    String OutputString = String.valueOf(Output);

    Num.setText(OutputString);

【问题讨论】:

  • 你的各种Output变量是什么类型(int、long、float、double)?
  • 应该在代码的前面声明它已被声明为双精度。但是现在都解决了,谢谢!
  • @user1150531 你以前试过我的回答吗?我似乎与您接受的答案没有任何区别。

标签: android format decimal


【解决方案1】:

试试这个:

String OutputString = String.format("%.2f", Output);

Num.setText(OutputString);

String.format() 以确保您的输出中只有 2 位小数。

【讨论】:

    【解决方案2】:

    请试试这个:

    double Output = (Output1 / (1 -(Output2/100d)))
    Num.setText(String.format("%.2f",Output));
    

    希望这能解决您的问题。 最好的问候

    【讨论】:

    • 传奇,非常感谢你,我准备放弃了!只是一个问题 - 100 之后的 d 有什么作用?
    • 它表示 100 是双精度数,只是为了在计算时强制它只使用双精度数:) 很高兴这对您有所帮助。编码愉快!
    【解决方案3】:

    如果你想限制'decimal_point'前后的位数,那么你可以使用我的解决方案。

    private class DecimalNumberFormatTextWatcher implements TextWatcher{
        int pos;
        int digitsBeforeDecimal = 6;
        int digitsAfterDecimal = 2;
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if(s.length() > 2)
                pos = start;
            else {
                pos = start + 2;
            }
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
    
        }
    
        @Override
        public void afterTextChanged(Editable s) {
            mEdittext.removeTextChangedListener(this);
            String text = s.toString();
            if(text!= null && !text.equals("")){
                if(!text.contains("$")){ //if it does not contains $
                    text = "$"+text;
                } else {
                    if (text.indexOf("$") > 0) { //user entered value before $
                        text = s.delete(0, text.indexOf("$")).toString();
                    }else {
                        if(!text.contains(".")){ // not a fractional value
                            if(text.length() > digitsBeforeDecimal+1) { //cannot be more than 6 digits
                                text = s.delete(pos, pos+1).toString();
                            }
                        } else { //a fractional value
                            if(text.indexOf(".") - text.indexOf("$") > digitsBeforeDecimal+1){ //non fractional part cannot be more than 6
                                text = s.delete(pos,pos+1).toString();
                            }
                            if((text.length() - text.indexOf(".")) > digitsAfterDecimal+1) { //fractinal part cannot be more than 2 digits
                                text = s.delete(text.indexOf(".") + 2, text.length() - 1).toString();
                            }
                        }
                    }
                }
            }
            mEdittext.setText(text);
            mEdittext.setSelection(pos);
            mEdittext.addTextChangedListener(this);
        }
    }
    
    mEdittext.addTextChangedListener(new DecimalNumberFormatTextWatcher());
    

    这也会在用户输入值时添加货币符号。

    希望这对任何人都有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2013-06-02
      相关资源
      最近更新 更多