我有一项活动会将订单金额与价格相加,并且还可以选择添加增值税。前大桶价格很好,总是加到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())))))))))))))))))))))))))))))))))))))))));
;};;});
}
答案 0 :(得分: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());
}
}
});
答案 1 :(得分:0)
我看到了两种可能性:
String.format("%.2f", yourValue);
它会将你的字符串舍入为2位小数。
否则,
答案 2 :(得分:0)
我强烈建议使用变量,它会让你的生活更轻松,
这是一个将double加倍到指定小数位数的实用程序。
例如:
round(123.456,2); //返回123.46