android - How to have decimal to int to decimal again? -


i developing app calculation. on tab have 3 edittexts. 1 edittext price input , 1 percentage of , last 1 result them both. got task make these edittexts shown currency(i using decimal format). able display result currency. price input need in decimal format, somehow code wont work(its bad,really bad implementation). idea make diplayed value decimal format meanwhile value being calculated , result displayed on edittext result box. further task make these 3 edittext auto generated user input value anywhere(could on price edittext , percentage or result , price edittext ) example:

price edittext: 100000 percentage 30 result:  30000 (auto generate itself(means fill value)) 

or

price edittext 100000 percentage:  30(auto generate itself(means fill value)) result 30000 

i able first 1 using textwatcher, code:

edittextforprice.addtextchangedlistener(new textwatcher() {             @override             public void ontextchanged(charsequence s, int start, int before,                     int count) {                 string text1 = edittextforprice.gettext().tostring();                 string text2 = percentageoftheprice.gettext().tostring();                  double input1 = 0;                 double input2 = 0;                 if (text1.length() > 0)                  input1 = double.valueof(text1);                 if (text2.length() > 0)                     input2 = double.valueof(text2);                 if (text1.length() != 0) {                      double output = (input1 * input2) / 100;                     e = kursindonesia.format(output);                     outputtumbal = e;                     edittextresult.settext("" + e);                 } else if (text2.length() == 0) {                     edittextresult.settext("");                 }             } 

to avoid error add above codes on percentage textwatcher, when user edit input app not crash(for example erase input data, or become zero)

this 1 converter, put on oncreate

final decimalformat kursindonesia = (decimalformat) decimalformat                 .getcurrencyinstance();         decimalformatsymbols formattedstuff = new decimalformatsymbols();         formattedstuff.setcurrencysymbol("rp. ");         formattedstuff.setmonetarydecimalseparator(',');         formattedstuff.setgroupingseparator('.');         kursindonesia.setdecimalformatsymbols(formattedstuff); 

the result edittext diplay result :

result: rp. 30.000,00 

is possible use

final decimalformat kursindonesia = (decimalformat) decimalformat                     .getcurrencyinstance();             decimalformatsymbols formattedstuff = new decimalformatsymbols();             formattedstuff.setcurrencysymbol("rp. ");             formattedstuff.setmonetarydecimalseparator(',');             formattedstuff.setgroupingseparator('.');             kursindonesia.setdecimalformatsymbols(formattedstuff); 

and

decimalformat dec1 = new decimalformat(); dec1.setgroupingused(false); 

on textwatcher code above want convert given value(user input) decimal on display value calculated in textwatcher above. did this

edittextforprice.addtextchangedlistener(new textwatcher() {             @override             public void ontextchanged(charsequence s, int start, int before,                     int count) {                         string text1 = ethint1.gettext().tostring();                     decimalformat kursindonesia1 = (decimalformat) decimalformat                             .getcurrencyinstance();                     decimalformatsymbols formattedstuff1 = new decimalformatsymbols();                     formattedstuff1.setcurrencysymbol("rp. ");                     formattedstuff1.setmonetarydecimalseparator(',');                     formattedstuff1.setgroupingseparator('.');                     kursindonesia1.setdecimalformatsymbols(formattedstuff1);                     if (text1.length() != 0) {                         //long lol = long.parselong(text1);                         e = kursindonesia1.format(text1);                         ethint1.settext("" + e);                     }                 }             } 

to convert given value currency wont work, , tried this:

edittextforprice.addtextchangedlistener(new textwatcher() {             @override             public void ontextchanged(charsequence s, int start, int before,                     int count) {                 decimalformat dec = new decimalformat();                 dec.setgroupingused(false);                 string text1 = edittextforprice.gettext().tostring();                 string text2 = edittextpercentage.gettext().tostring();                 string a1 = null;                 double input1 = 0;                 double input2 = 0;                 if (text1.length() > 0)                 a1 = dec.format(text1);                 input1 = double.valueof(text1);                 if (text2.length() > 0)                     input2 = double.valueof(text2);                 if (text1.length() != 0) {                     a1 = dec.format(text1);                 input1 = long.parselong(a1);                     double output = (input1 * input2) / 100;                     e = kursindonesia.format(output);                     outputtumbal = e;                     textvdp1.settext("" + e);                 } else if (text2.length() == 0) {                     textvdp1.settext("");                 }             } 

this wont work, how implement ideas correctly? in advance


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -