DecimalFormatted字符串的怪异行为

时间:2019-06-25 09:58:31

标签: java android

我目前正在尝试制作一个自动在其中保留两位小数的计算器,然后按一个编号的Button将其添加到TextView中显示的值中,同时保持两位小数。例如,如果按下1,则该值应为0.01。但是似乎我发现String的行为很奇怪,我不确定我目前正在做什么。

这是我的相关代码

public void keyPressed(View v){
    String btnTxtVal = ((Button) v).getText().toString();
    String lblTxtVal = lblInput.getText().toString();

    inputTxt.append(btnTxtVal);
    temp = Double.parseDouble(inputTxt.toString()) / 100;

    lblInput.setText(decimalFormat.format(temp));
    Log.d(TAG, "btn: "+btnTxtVal+" temp: "+temp);
    Log.d(TAG, "formattedTemp: "+decimalFormat.format(temp));
}

上面的代码通过DataBinding调用,因为它位于Fragment中。

问题在于,当我按下数字Button之后按下数字Button时,由于某种原因,数字前面会添加一个额外的零。

这就是我的日志。

pressedBtn: 1 temp: 0.01
formattedTemp: 0.01

pressedBtn: 2 temp: 0.12
formattedTemp: 0.12

pressedBtn: 3 temp: 1.23
formattedTemp: 1.23

pressedBtn: 4 temp: 12.34
formattedTemp: 12.34

pressedBtn: 00 temp: 1234.0
formattedTemp: 1,234

pressedBtn: 0 temp: 12340.0
formattedTemp: 12,340

pressedBtn: 3 temp: 123400.03
formattedTemp: 123,400.03

这是我当前正在使用的代码。

public class KeypadFragment extends Fragment {
    private View rootView;
    private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btn00, btnDelete, btnPlus;
    private TextView lblInput;
    private Double firstInput = 0.00, secondInput = 0.00, temp = 0.00, currentSum = 0.00;
    private DecimalFormat decimalFormat = new DecimalFormat("#,###.##");
    private StringBuilder inputTxt = new StringBuilder();
    String TAG = "sharePOS";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_keypad, container, false);

        btn1 = rootView.findViewById(R.id.btnKeypad1);
        btn2 = rootView.findViewById(R.id.btnKeypad2);
        btn3 = rootView.findViewById(R.id.btnKeypad3);
        btn4 = rootView.findViewById(R.id.btnKeypad4);
        btn5 = rootView.findViewById(R.id.btnKeypad5);
        btn6 = rootView.findViewById(R.id.btnKeypad6);
        btn7 = rootView.findViewById(R.id.btnKeypad7);
        btn8 = rootView.findViewById(R.id.btnKeypad8);
        btn9 = rootView.findViewById(R.id.btnKeypad9);
        btn0 = rootView.findViewById(R.id.btnKeypad0);
        btn00 = rootView.findViewById(R.id.btnKeypad00);
        btnDelete = rootView.findViewById(R.id.btnKeypadDelete);
        btnPlus = rootView.findViewById(R.id.btnKeypadAdd);
        lblInput = rootView.findViewById(R.id.lblKeypadInput);

        FragmentKeypadBinding binding = DataBindingUtil.bind(rootView);
        binding.setFragment(this);

        btnPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lblInput.setText("0.00");

                inputTxt = new StringBuilder();

                if(firstInput == 0.00){
                    firstInput = Double.parseDouble(lblInput.getText().toString());
                }else if(secondInput == 0.00){
                    secondInput = Double.parseDouble(lblInput.getText().toString());

                    firstInput += secondInput;
                    secondInput = 0.00;
                }

            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lblInput.setText("0.00");

                inputTxt = new StringBuilder();
                firstInput = 0.00;
                secondInput = 0.00;
            }
        });

        return rootView;
    }

    public void keyPressed(View v){
        String btnTxtVal = ((Button) v).getText().toString();
        String lblTxtVal = lblInput.getText().toString();

        inputTxt.append(btnTxtVal);
        temp = Double.parseDouble(inputTxt.toString()) / 100;

        lblInput.setText(decimalFormat.format(temp));
        Log.d(TAG, "btn: "+btnTxtVal+" temp: "+temp);
        Log.d(TAG, "formattedTemp: "+decimalFormat.format(temp));
    }

}

我的布局有单独的000 Button

1 个答案:

答案 0 :(得分:0)

您显示的所有代码似乎都可以正常工作。这里有一个基于您的代码的微型示例项目,证明它可以正常工作:

class Main{
  public static void main(String[] a){
    String lblTextVal = "0.00";
    String inputText = "";
    System.out.println("Starting with: lblTextVal: "+lblTextVal + " inputText: \""+inputText+"\"");
    System.out.println();

    java.text.NumberFormat decimalFormat = new java.text.DecimalFormat("#,##0.00");

    for(String btnPress : new String[]{"1","2","3","4","0","0","3"}){
      inputText += btnPress;                    // inputTxt.append(btnTxtVal);
      Double temp = Double.parseDouble(inputText.toString()) / 100;
      lblTextVal = decimalFormat.format(temp);  // lblInput.setText(decimalFormat.format(temp));

      System.out.println("pressedBtn: "+btnPress+" temp: "+temp);
      System.out.println("formattedTemp: "+decimalFormat.format(temp));
      System.out.println();
    }
  }
}

Try it online.

日志中的怪异部分是最后三个部分:

pressedBtn: 00 temp: 1234.0
formattedTemp: 1,234

pressedBtn: 0 temp: 12340.0
formattedTemp: 12,340

pressedBtn: 3 temp: 123400.03
formattedTemp: 123,400.03

应该是哪个:

pressedBtn: 0 temp: 123.4
formattedTemp: 123.40

pressedBtn: 0 temp: 1234.0
formattedTemp: 1,234.00

pressedBtn: 3 temp: 12340.03
formattedTemp: 12,340.03

根据您的代码,我只看到三个可能的原因之一:

  1. 您的decimalFormat不正确。
  2. 由于某种原因,您有一个按钮00,该按钮一次附加了两个00,而不是一个0
  3. 您将inputTxt附加在两者之间的其他位置。

最可能的原因可能是您的日志似乎反映的选项2。尽管选项1也绝对是不正确的,但由于formattedTemp在倒数第二个和倒数第三个部分(上面我复制的三个中的前两个)中缺少十进制值。

相关问题