java - Using DecimalFormat with Android Development -


i have looked @ ways solve still new java , looking , kind of diving head first , ran problem. problem having want change number of decimal places output "tip calculator" making , want format in currency using $ symbol. code below 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 activity 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 have output of equation , use money.format , call textview used , format decimalformat had. instead app force closes when hit calculate button log errors

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 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):     @ java.text.numberformat.format(numberformat.java:308)  06-04 21:32:57.257: e/androidruntime(1905):     @  java.text.decimalformat.format(decimalformat.java:714)  06-04 21:32:57.257: e/androidruntime(1905):     @ java.text.format.format(format.java:93)  06-04 21:32:57.257: e/androidruntime(1905):     @ com.verge.tipcalc.tipcalculatoractivity$1.onclick(tipcalculatoractivity.java:50)  06-04 21:32:57.257: e/androidruntime(1905):     @ android.view.view.performclick(view.java:2537)  06-04 21:32:57.257: e/androidruntime(1905):     @ android.view.view$performclick.run(view.java:9157)  06-04 21:32:57.257: e/androidruntime(1905):     @  android.os.handler.handlecallback(handler.java:587)  06-04 21:32:57.257: e/androidruntime(1905):     @ android.os.handler.dispatchmessage(handler.java:92)  06-04 21:32:57.257: e/androidruntime(1905):     @ android.os.looper.loop(looper.java:130)  06-04 21:32:57.257: e/androidruntime(1905):     @ android.app.activitythread.main(activitythread.java:3687)  06-04 21:32:57.257: e/androidruntime(1905):     @ java.lang.reflect.method.invokenative(native method)  06-04 21:32:57.257: e/androidruntime(1905):     @ java.lang.reflect.method.invoke(method.java:507)  06-04 21:32:57.257: e/androidruntime(1905):     @  com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:842)  06-04 21:32:57.257: e/androidruntime(1905):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:600)  06-04 21:32:57.257: e/androidruntime(1905):     @ dalvik.system.nativestart.main(native method) 

all want round totalbill , tiptotal 2 decimal places , have formatted in currency have tried 3-4 methods looked , none of them have been successful.

lyou need -

decimalformat money = new decimalformat("00.00"); money.setroundingmode(roundingmode.up);// can use down here if dont need round decimal. . . system.out.println(money.format(new double(totalbill.gettext()))); 

whats important here think missed new double(totalbill.gettext()) . cannot pass totalbill directly textview. need parse double or flot , pass formatter.

also $ can appended later text.

totalbill.settext("$" + totalbill.gettext()) 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -