Math not producing correct outcome

  1. 7 months ago

    Started with web a little math problem

    I have a cash textfield and a Loan textfield.
    The cash field starts with 4000000 and I want the cash field automatically update as I input the Loan amount in the Loan textfield.
    When I input 2000000 in Loan field, I got an updat of "2.000000e+6" instead of "2000000".
    Is this a formatting problem?

    use FORMAT not STR

  2. Dave S

    9 Oct 2018 San Diego, California USA

    depends.... did you format it? in what manner did you tell you app to display the value?
    default formatting of double values, will go to SciNot format when certain circumstances occur....

    so , no, it probably is not a "problem"... it is most likely the app doing exactly what would be considered normal behaviour.

  3. So how should I format those 2 fields so that I can get a Cash= 4000000 - Loan=2000000, and having Cash update and show 2000000?

    The following give me the Sci-representation
    Cash.text = str(val(Cash.text) - val(Loan.text))

  4. Dave S

    9 Oct 2018 Answer San Diego, California USA

    use FORMAT not STR

  5. David C

    10 Oct 2018 Pre-Release Testers, Xojo Pro Derby, ITM

    It may be overkill, but after I was bitten by this STR() issue I now use this method to return an Integer as a String.

    Protected Function getLargeIntegerWAD(largeNumber As Double, decimalPoints As Integer = 0, formatString As String = "-####################0") as String
      Dim returnResult As String
      if decimalPoints > 0 and instr(formatString, ".") = 0 then 'don't add two periods!
        formatString = formatString + "."
        for tempInt As Integer = 0 to decimalPoints
          formatString = formatString + "0"
      end if
      returnResult = Format(largeNumber, formatString)
      if instr(formatString, ".") > 0 then 'don't remove the final zero if it's not a decimal!
        while right(returnResult, 1) = "0" and NthField(returnResult, ".", 2).len > decimalPoints
          returnResult = left(returnResult, len(returnResult) - 1) 'remove trailing zeros
      end if
      if right(returnResult, 1) = "." then returnResult = left(returnResult, len(returnResult) - 1) 'remove trailing period
      Return returnResult
    End Function

or Sign Up to reply!