android - How do I convert decimal input into an int? -
i have edittext field user can enter weight in kilogram, i.e. 11.7. want store in database gramms, i.e. 11700.
how convert string "11.7" integer 11700?
to value edittext
use http://developer.android.com/reference/android/widget/edittext.html#gettext()
string userinput = edittext.gettext().tostring();
to convert string double use code @govindpatel http://developer.android.com/reference/java/lang/double.html#parsedouble(java.lang.string)
try { double kg = double.parsedouble(userinput) } catch (numberformatexception nfe) { edittext.seterror(r.string.invalid_double); // or whatever want show user }
to convert kg g:
long gramms = math.round(kg * 1000);
you replace double , long float , int if know don't need precision or range of larger variables.
Comments
Post a Comment