/ Forums / Advansys Formativ / Creating Solutions with Formativ / decimals in text fields or masked fields

  • Creator
    Topic
  • #4236
    embru
    Participant

      I’m adding to field values with decimals. The sum Variable seems to be ok but when i load the result in a text field, the result has more decimals…

      x=CSng(field1.text): field contains 10
      x=x+CSng(field2.text): field contains 4.04

      msgbox x: result is 14.04

      field3.text=x: result in field3.text is 14.039999996….

      what’s wrong?

    • Author
      Replies
    • #7564
      Support 3
      Participant

        CSng function return single data type and you need to convert Single to String. Use the CStr function to returns an expression that has been converted to a variant of subtype string.

        field3.text = cstr(x)
        

        Hope this helps.

        Regards,
        Advansys Support

        #7563
        Support 1
        Participant

          In general, programming languages store floating-point numbers in approximate form. This means the value that you write to a floating-point variable X may be slightly different to the value that you read from X.

          The VBScript function Round() may be what you are looking for. Here is a sample of usage:

            field3.text = Round(x, 2)

          For more information on the inexactness of floating-point representation in computing, see http://en.wikipedia.org/wiki/Floating_Point#Problems_with_floating-point.

          Regards,
          Advansys Support

          #7561
          embru
          Participant

            Thanks Support3 – re-convertig to a string was the reason.

            Thanks

            #7562
            Support 3
            Participant

              Thanks for your feedbacks.

              Regards,
              Advansys Support

            Viewing 4 replies - 1 through 4 (of 4 total)
            • You must be logged in to reply to this topic.