0

我有一项活动,可以将价格与订单数量相加,并且还可以选择在此基础上添加增值税。exvat 价格很好,总是加到小数点后 2 位。然而,当添加增值税(英国为 20%)时,它最终会超过小数点后 2 位。我已经看到了其他建议,但无法使其正常工作。任何帮助将不胜感激。我的代码如下

        editText1 = (EditText) findViewById(R.id.editText1);
    editText2 = (EditText) findViewById(R.id.editText2);
    editText3 = (EditText) findViewById(R.id.editText3);
    editText4 = (EditText) findViewById(R.id.editText4);
    editText5 = (EditText) findViewById(R.id.editText5);
    editText6 = (EditText) findViewById(R.id.editText6);
    editText7 = (EditText) findViewById(R.id.editText7);
    editText8 = (EditText) findViewById(R.id.editText8);
    editText9 = (EditText) findViewById(R.id.editText9);
    editText10 = (EditText) findViewById(R.id.editText10);
    editText11 = (EditText) findViewById(R.id.editText11);
    editText12 = (EditText) findViewById(R.id.editText12);
    editText13 = (EditText) findViewById(R.id.editText13);
    editText14 = (EditText) findViewById(R.id.editText14);
    editText15 = (EditText) findViewById(R.id.editText15);
    editText16 = (EditText) findViewById(R.id.editText16);
    editText17 = (EditText) findViewById(R.id.editText17);
    editText18 = (EditText) findViewById(R.id.editText18);
    editText19 = (EditText) findViewById(R.id.editText19);
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);

    button1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (editText19.getText() != null || editText18.getText() != null || editText17.getText() != null || editText16.getText() != null || editText15.getText() != null || editText14.getText() != null || editText13.getText() != null || editText12.getText() != null || editText11.getText() != null || editText10.getText() != null || editText9.getText() != null || editText8.getText() != null || editText7.getText() != null || editText6.getText() != null || editText5.getText() != null || editText4.getText() != null || editText3.getText() != null)
                editText2.setText((String.valueOf(11.67 * (Double.parseDouble(editText16.getText().toString())) + ((15.83 * (Double.parseDouble(editText17.getText().toString())) + ((18.33 * (Double.parseDouble(editText18.getText().toString())) + ((18.33 * (Double.parseDouble(editText18.getText().toString())) + ((24.17 * (Double.parseDouble(editText19.getText().toString())) + ((22.36 * (Double.parseDouble(editText15.getText().toString())) + ((4.17 * (Double.parseDouble(editText14.getText().toString())) + ((26.66 * (Double.parseDouble(editText13.getText().toString())) + ((4.17 * (Double.parseDouble(editText12.getText().toString())) + ((30.96 * (Double.parseDouble(editText11.getText().toString())) + ((4.17 * (Double.parseDouble(editText10.getText().toString())) + ((35.26 * (Double.parseDouble(editText9.getText().toString())) + ((4.17 * (Double.parseDouble(editText8.getText().toString())) + ((39.56 * (Double.parseDouble(editText7.getText().toString())) + ((4.17 * (Double.parseDouble(editText6.getText().toString())) + ((9.17 * (Double.parseDouble(editText5.getText().toString())) + ((13.33 * (Double.parseDouble(editText4.getText().toString())) + ((17.20 * (Double.parseDouble(editText3.getText().toString())))))))))))))))))))))))))))))))))))))));


    ;};;});

    button2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (editText19.getText() != null || editText18.getText() != null || editText17.getText() != null || editText16.getText() != null || editText15.getText() != null || editText14.getText() != null || editText13.getText() != null || editText12.getText() != null || editText11.getText() != null || editText10.getText() != null || editText9.getText() != null || editText8.getText() != null || editText7.getText() != null || editText6.getText() != null || editText5.getText() != null || editText4.getText() != null || editText3.getText() != null)
                editText1.setText((String.valueOf(1.2 * ((11.67 * (Double.parseDouble(editText16.getText().toString())) + ((15.83 * (Double.parseDouble(editText17.getText().toString())) + ((18.33 * (Double.parseDouble(editText18.getText().toString())) + ((18.33 * (Double.parseDouble(editText18.getText().toString())) + ((24.17 * (Double.parseDouble(editText19.getText().toString())) + ((22.36 * (Double.parseDouble(editText15.getText().toString())) + ((4.17 * (Double.parseDouble(editText14.getText().toString())) + ((26.66 * (Double.parseDouble(editText13.getText().toString())) + ((4.17 * (Double.parseDouble(editText12.getText().toString())) + ((30.96 * (Double.parseDouble(editText11.getText().toString())) + ((4.17 * (Double.parseDouble(editText10.getText().toString())) + ((35.26 * (Double.parseDouble(editText9.getText().toString())) + ((4.17 * (Double.parseDouble(editText8.getText().toString())) + ((39.56 * (Double.parseDouble(editText7.getText().toString())) + ((4.17 * (Double.parseDouble(editText6.getText().toString())) + ((9.17 * (Double.parseDouble(editText5.getText().toString())) + ((13.33 * (Double.parseDouble(editText4.getText().toString())) + ((17.20 * (Double.parseDouble(editText3.getText().toString())))))))))))))))))))))))))))))))))))))))));
            ;};;});



}
4

3 回答 3

2

通常我使用 BigDecimal 来缩放十进制值并将其显示给用户。让我们为 editText1 执行此操作:

button2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                if (editText19.getText() != null || editText18.getText() != null || editText17.getText() != null || editText16.getText() != null || editText15.getText() != null || editText14.getText() != null || editText13.getText() != null || editText12.getText() != null || editText11.getText() != null || editText10.getText() != null || editText9.getText() != null || editText8.getText() != null || editText7.getText() != null || editText6.getText() != null || editText5.getText() != null || editText4.getText() != null || editText3.getText() != null) {
                    // Create a big decimal with your value
                    BigDecimal bd = new BigDecimal(1.2 * ((11.67 * (Double.parseDouble(editText16.getText().toString())) + ((15.83 * (Double.parseDouble(editText17.getText().toString())) + ((18.33 * (Double.parseDouble(editText18.getText().toString())) + ((18.33 * (Double.parseDouble(editText18.getText().toString())) + ((24.17 * (Double.parseDouble(editText19.getText().toString())) + ((22.36 * (Double.parseDouble(editText15.getText().toString())) + ((4.17 * (Double.parseDouble(editText14.getText().toString())) + ((26.66 * (Double.parseDouble(editText13.getText().toString())) + ((4.17 * (Double.parseDouble(editText12.getText().toString())) + ((30.96 * (Double.parseDouble(editText11.getText().toString())) + ((4.17 * (Double.parseDouble(editText10.getText().toString())) + ((35.26 * (Double.parseDouble(editText9.getText().toString())) + ((4.17 * (Double.parseDouble(editText8.getText().toString())) + ((39.56 * (Double.parseDouble(editText7.getText().toString())) + ((4.17 * (Double.parseDouble(editText6.getText().toString())) + ((9.17 * (Double.parseDouble(editText5.getText().toString())) + ((13.33 * (Double.parseDouble(editText4.getText().toString())) + ((17.20 * (Double.parseDouble(editText3.getText().toString())))))))))))))))))))))))))))))))))))))));
                    // Round value to 2 decimal
                    bd = bd.setScale(2, RoundingMode.HALF_UP);
                    editText1.setText(bd.toString());
                 }
            }
        });
于 2013-08-22T13:49:16.507 回答
0

我看到了两种可能性:

String.format("%.2f", yourValue);

它会将您的字符串四舍五入到小数点后 2 位。

要不然,

  • 乘以 100
  • 四舍五入为整数值(或 ceil 或 floor)
  • 再次除以 100 以浮动
于 2013-08-22T13:36:09.403 回答
0

我强烈建议使用变量,它会让你的生活更轻松,

这是一个将双精度数舍入到指定小数位数的实用程序。

例如:

round(123.456, 2);// 返回 123.46

于 2013-08-22T13:40:03.180 回答