[英]Using DecimalFormat with Android Development
我已經研究了解決此問題的方法,但我對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
小數點后tiptotal
到小數點后totalbill
並將其tiptotal
為貨幣格式,我嘗試了3-4種方法,但都沒有成功。
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,然后將其傳遞給formatter。
$
也可以稍后添加到文本中。
totalbill.setText("$" + totalbill.getText())
你可以試試這個代碼
這樣轉換ur BigDecimal值,使其十進制值最多為2個位
BigDecimal bd = new BigDecimal( costNum);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.