Android Question How to deal with EditText during calculations

Discussion in 'Android Questions' started by Alberto Michelis, Jun 10, 2015.

  1. Alberto Michelis

    Alberto Michelis Active Member Licensed User

    Hi,
    I have many EditText which are numbers and I deal with it all the time no knowing what todo having many error of double"" and so on, no I got an unexpected Array, see image please and I just dont know how to program a simple caculation.

    A routine like:

    Code:
    Sub CalcTot
       
    If eDias.Text>0 And eTasa.Text>0 Then
         
    Dim d As Int
         d=eDias.Text
         
    DateTime.DateFormat="dd/MM/yyyy"
         dVto.Text = 
    DateTime.Date(DateTime.Add(DateTime.Now,0,0,d))
         eIntereses.Text = (ePrecio.Text * eCantidad.Text) * (eTasa.Text/
    100) * d /365
         eIntereses.Text = 
    NumberFormat2(eIntereses.Text,0,2,2,True)
       
    End If
       dTotal.Text = (ePrecio.Text * eCantidad.Text) + eIntereses.Text
       dTotal.Text = 
    NumberFormat2(dTotal.Text,0,2,2,True)
    can give an infinite number of errors if one of the components is "" or null or...

    How must I code this?

    Thanks
     
  2. NJDude

    NJDude Expert Licensed User

    Search the forum for "IsNumber", that will give you a clue.
     
  3. LucaMs

    LucaMs Expert Licensed User

    Some suggestions.

    1) do not use views for calculations.
    It is better to use variables, ALSO to keep separate GUI from the code;
    So you should have:
    Dim Dias As Int
    Dias = eDias.Text

    2) Verify the user input (in eDias, for example) and set the EditText.InputType

    3) if you really want to test this way:
    If eDias.Text > 0
    you can use:
    If "0" & eDias.Text > 0
    given that a zero on the left does not change the numeric value.
     
  4. LucaMs

    LucaMs Expert Licensed User

    BTW year has 365.25 days ;)
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    You should use DateUtils for such calculations. Date / time arithmetics are much more complicated than it may first seem.
     
  6. Alberto Michelis

    Alberto Michelis Active Member Licensed User

    Thanks Lucas, the idea of use
    Dim Dias As Int
    Dias = eDias.Text
    that means a Dim and an assignment for each edittext in each activity... does it have any impact on perfomance?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    No. It will not have any effect on the performance.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice