I have looked at ways to solve this I am still very new to java and just looking into it and am kind of diving head first into it and ran into a problem. The problem I am having is that I want to change the number of Decimal places that is output by this "Tip Calculator" I am making and I also want it to format in currency using the $ symbol. My code is below I am using
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));
}
});
}
}
My method of thinking was basically that I could have the output of the equation and then I could use the money.format
and call the textview
that I used and it would format it to the DecimalFormat
that I had. Instead now the app force closes when I hit the calculate button the log errors I get are
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)
All I want to do is round the totalbill
and the tiptotal
to 2 decimal places and have it formatted in currency I have tried like 3-4 methods I looked up and none of them have been successful.
lYou will need to do it like this -
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())));
Whats important here i think you missed is new Double(totalbill.getText())
. You cannot pass totalBill directly as it is textView. You will need to parse it into Double or Flot and than pass it to formatter.
Also $
can be appended later to the text.
totalbill.setText("$" + totalbill.getText())
u can try this code
Convert ur BigDecimal value like this so that its decimal value will be upto 2 places
BigDecimal bd = new BigDecimal( costNum);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.