2013-08-22 122 views
0

我有一個活動將價格與訂單數量相加,並且還可以選擇在其上添加增值稅。價格很好,總是增加到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()))))))))))))))))))))))))))))))))))))))))); 
      ;};;}); 



} 

回答

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()); 
       } 
      } 
     }); 
+0

這引發了一些錯誤。 bd之前的BigDecimal需要a;完成我覺得奇怪的事情。它還爲我提供了一些快速修復,通常爲BigDecimal創建一個字段。 – j1mmyg88

+0

對不起支架後,如果 – Mathroule

+0

你是先生,是一個LEG結束,muchos gracias senor – j1mmyg88

0

我看到兩個可能性:

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

它會圓你的字符串到小數點後2。

否則,

  • 乘以100
  • 一輪整數值(或小區或樓)
  • 除以100再漂浮
+1

編輯,只是讀它說你的價值。需要一些咖啡。 – j1mmyg88

+0

它將採取「yourValue」並正確顯示,所以對我來說,計算後。但yourValue仍然具有所有的數字,只有顯示的字符串會帶有2位數字。如果數字需要四捨五入爲2位數,請使用我的第二個建議。 – HpTerm

+0

+1 ;-)我現在去一個,你可以加入(順便說一句,不是咖啡)。 – HpTerm

0

我強烈建議使用變量,它會讓你的生活變得更加輕鬆,並且這個工具將一個double加到指定的數字上的小數位。

例如:

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

+0

我不知道接受數字作爲參數的圓形方法。那是一個標準的java/android庫嗎?我會測試一下。 – HpTerm

+0

我已經輸入,在計算之前和計算之後嘗試過。而這仍然不起作用 – j1mmyg88

相關問題