0

我已经研究了解决这个问题的方法,我对 java 还是很陌生,只是在研究它,我有点想先进入它,然后遇到了问题。我遇到的问题是我想更改我正在制作的这个“提示计算器”输出的小数位数,并且我还希望它使用 $ 符号以货币格式格式化。我的代码在下面我正在使用

public class TipCalculatorActivity extends Activity {

      DecimalFormat money = new DecimalFormat("$0.00");
      EditText costofmeal, tippercent;
      TextView tiptotal, totalbill; 
      Button calctipbtn;
      BigDecimal costNum, percentNum, billNum;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            calctipbtn = (Button) findViewById(R.id.calctipbtn);
            costofmeal = (EditText) findViewById(R.id.CostofMeal); 
            tippercent = (EditText) findViewById(R.id.PercentTipText);
            tiptotal = (TextView) findViewById(R.id.tiptotal);       
            totalbill = (TextView) findViewById(R.id.totalbill);   
            calctipbtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                costNum = new BigDecimal(costofmeal.getText().toString());
                percentNum = new BigDecimal(tippercent.getText().toString());

                tiptotal.setText(costNum.multiply(percentNum).toString());
                System.out.println(money.format(tiptotal));

                costNum = new BigDecimal(costofmeal.getText().toString());
                billNum = new BigDecimal(tiptotal.getText().toString());


                totalbill.setText(costNum.add(billNum).toString());
                System.out.println(money.format(totalbill));
            }
          });   
     }
} 

我的思维方法基本上是我可以得到方程的输出,然后我可以使用money.format并调用textview我使用的,它会将它格式化为DecimalFormat我所拥有的。相反,当我点击计算按钮时,应用程序强制关闭,我得到的日志错误是

06-04 21:32:57.242: D/AndroidRuntime(1905): Shutting down VM

06-04 21:32:57.242: W/dalvikvm(1905): threadid=1: thread exiting with uncaught exception    (group=0x40015578)

06-04 21:32:57.257: E/AndroidRuntime(1905): FATAL EXCEPTION: main

06-04 21:32:57.257: E/AndroidRuntime(1905): java.lang.IllegalArgumentException

06-04 21:32:57.257: E/AndroidRuntime(1905):     at java.text.NumberFormat.format(NumberFormat.java:308)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at 
java.text.DecimalFormat.format(DecimalFormat.java:714)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at java.text.Format.format(Format.java:93)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at com.verge.tipcalc.TipCalculatorActivity$1.onClick(TipCalculatorActivity.java:50)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at android.view.View.performClick(View.java:2537)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at android.view.View$PerformClick.run(View.java:9157)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at 
android.os.Handler.handleCallback(Handler.java:587)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at android.os.Handler.dispatchMessage(Handler.java:92)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at android.os.Looper.loop(Looper.java:130)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at android.app.ActivityThread.main(ActivityThread.java:3687)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at java.lang.reflect.Method.invokeNative(Native Method)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at java.lang.reflect.Method.invoke(Method.java:507)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)

06-04 21:32:57.257: E/AndroidRuntime(1905):     at dalvik.system.NativeStart.main(Native Method)

我想要做的就是将totalbill和舍入tiptotal到小数点后 2 位,并将其格式化为我尝试过的 3-4 种方法,但都没有成功。

4

2 回答 2

1

l 你需要这样做——

DecimalFormat money = new DecimalFormat("00.00");
money.setRoundingMode(RoundingMode.UP);// you can also use DOWN here if you dont need to round up the decimal.
.
.
System.out.println(money.format(new Double(totalbill.getText())));

我认为你错过的重要的是new Double(totalbill.getText())。您不能直接传递 totalBill,因为它是 textView。您需要将其解析为 Double 或 Flot,然后将其传递给格式化程序。

$可以稍后附加到文本中。

totalbill.setText("$" + totalbill.getText())
于 2012-06-05T04:49:56.040 回答
0

你可以试试这个代码

像这样转换您的 BigDecimal 值,使其十进制值最多为 2 位

   BigDecimal bd = new BigDecimal( costNum);
   bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
于 2012-06-05T04:41:30.927 回答