java - Hot to show only two digits after decimal using double -


i use:

pricetextview.settext(string.format("%.2f",price)); 

price double. works when have retrieve value textview , convert double following error:

java.lang.numberformatexception: invalid double: "1,2"

is there way same thing , avoid error?

here code complete:

double prezzodouble = double.parsedouble(this.prezzo); prezzotextview.settext(string.format("%.2f", prezzodouble));  quantitasceltaedittext.addtextchangedlistener(new textwatcher() {      string prezzounitario=prezzo;      public void aftertextchanged(editable s) {          int quantitasceltaint = integer.parseint(quantitasceltaedittext.gettext().tostring());          if(quantitasceltaint>=1) {             string prezzototale = string.valueof(string.format ("%.2f", (calcoloprezzototale())));             prezzotextview.settext(prezzototale);         }         else         {             prezzotextview.settext(prezzounitario);         }     }      public void beforetextchanged(charsequence s, int start, int count, int after) {     }      public void ontextchanged(charsequence s, int start, int before, int count) {     }      private double calcoloprezzototale()     {         double prezzonum=double.parsedouble(prezzotextview.gettext().tostring()); ///////////         double prezzototale=0;          int quantitasceltanum = integer.parseint(quantitasceltaedittext.gettext().tostring());          prezzototale = prezzonum * quantitasceltanum;          return prezzototale;     } }); 

it looks double assigned in line

prezzototale = string.valueof(string.format ("%.2f", (calcoloprezzototale()))); 

contains comma (,, i.e. 1,2) due locale settings. take care parsing happens same locale.

class numberformat helps out:

numberformat format = numberformat.getinstance(locale.france); number number = format.parse("1,234"); double d = number.doublevalue(); 

looking @ code locale.italy ;) check full locale list here.

p.s.: string.format() uses locale.getdefault(); per documentation. use logcat check value, if not sure system setting or use alternative method public static string format(locale l, string format, object... args) allows specify locale.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -