VB 6.3 Int Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Prior

    #1

    VB 6.3 Int Problem

    Since the last update for office 2003, i've been having issues with the int()
    function in vb (version 9972 vba retail 6.4.992)

    The accounting software that the business i work for writes for dozens of
    clients regularly uses x = (int(x*100))/100 to ensure that there are no
    fractional cents on the end of prices. It works wonderfully, until one of a
    number of values are used. 8.95, 9.95, 2094.95, and a host of other seemingly
    random numbers. For these, the end value of x computes as 8.94, 9.94 etc. Is
    this something other people have come across??
  • Cor Ligthert [MVP]

    #2
    Re: VB 6.3 Int Problem

    Chris,

    Maybe I read your message wrong, however this is a dotNet VB language
    newsgroup.

    Probably do you need help on Excel, for which is a very active programmer
    newsgroup.

    microsoft.publi c.excel.program mer

    If I am right wiht my assuption than I give you much more change on your
    question than here.

    Cor


    Comment

    • Cor Ligthert [MVP]

      #3
      Re: VB 6.3 Int Problem

      Chris,

      Maybe I read your message wrong, however this is a dotNet VB language
      newsgroup.

      Probably do you need help on Excel, for which is a very active programmer
      newsgroup.

      microsoft.publi c.excel.program mer

      If I am right wiht my assuption than I give you much more change on your
      question than here.

      Cor


      Comment

      • Andrew Taylor

        #4
        Re: VB 6.3 Int Problem


        Cor Ligthert [MVP] wrote:

        [Without quoting - Original post was about problem with
        x = (int(x*100))/100, where 8.95 >>> 8.94 in Excel VBA]
        [color=blue]
        > Chris,
        >
        > Maybe I read your message wrong, however this is a dotNet VB language
        > newsgroup.
        >
        > Probably do you need help on Excel, for which is a very active programmer
        > newsgroup.
        >
        > microsoft.publi c.excel.program mer
        >
        > If I am right wiht my assuption than I give you much more change on your
        > question than here.
        >
        > Cor[/color]

        However, the same problem could occur in .NET:

        In Visual Studio Command window:
        ? 8.95*100
        894.99999999999 989
        ? 9.95*100
        994.99999999999 989


        As always with this type of problem, the answer is that you can't rely
        on floating point numbers to give exact results.


        Andrew Taylor

        Comment

        • Andrew Taylor

          #5
          Re: VB 6.3 Int Problem


          Cor Ligthert [MVP] wrote:

          [Without quoting - Original post was about problem with
          x = (int(x*100))/100, where 8.95 >>> 8.94 in Excel VBA]
          [color=blue]
          > Chris,
          >
          > Maybe I read your message wrong, however this is a dotNet VB language
          > newsgroup.
          >
          > Probably do you need help on Excel, for which is a very active programmer
          > newsgroup.
          >
          > microsoft.publi c.excel.program mer
          >
          > If I am right wiht my assuption than I give you much more change on your
          > question than here.
          >
          > Cor[/color]

          However, the same problem could occur in .NET:

          In Visual Studio Command window:
          ? 8.95*100
          894.99999999999 989
          ? 9.95*100
          994.99999999999 989


          As always with this type of problem, the answer is that you can't rely
          on floating point numbers to give exact results.


          Andrew Taylor

          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: VB 6.3 Int Problem

            "Andrew Taylor" <andrew.taylor@ cantab.net> schrieb:[color=blue]
            > As always with this type of problem, the answer is that you can't rely
            > on floating point numbers to give exact results.[/color]

            Additional information:

            <URL:http://groups.google.d e/group/microsoft.publi c.dotnet.langua ges.vb/msg/1d103c859080279 2>

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://classicvb.org/petition/>

            Comment

            • Herfried K. Wagner [MVP]

              #7
              Re: VB 6.3 Int Problem

              "Andrew Taylor" <andrew.taylor@ cantab.net> schrieb:[color=blue]
              > As always with this type of problem, the answer is that you can't rely
              > on floating point numbers to give exact results.[/color]

              Additional information:

              <URL:http://groups.google.d e/group/microsoft.publi c.dotnet.langua ges.vb/msg/1d103c859080279 2>

              --
              M S Herfried K. Wagner
              M V P <URL:http://dotnet.mvps.org/>
              V B <URL:http://classicvb.org/petition/>

              Comment

              • AMDRIT

                #8
                Re: VB 6.3 Int Problem

                Here is some more explaination that doesn't require deep thought

                Option Explicit

                Sub SimpleTest()

                Dim SomeNumberA As Double
                Dim SomeNumberB As Currency
                Dim SomeNumberC As Single

                SomeNumberA = 8.950568
                SomeNumberB = 8.950568
                SomeNumberC = 8.950568

                'Floating points are only usefull for scientific notation (Single and
                Double)
                Debug.Print "Single " & (Int(SomeNumber C * 100)) / 100 & " is a " &
                TypeName((Int(S omeNumberC * 100)) / 100)

                Debug.Print "Double " & (Int(SomeNumber A * 100)) / 100 & " is a " &
                TypeName((Int(S omeNumberA * 100)) / 100)

                Debug.Print "Rounded Double " & (Int(Round(Some NumberA, 2) * 100)) / 100 & "
                is a " & TypeName((Int(R ound(SomeNumber A, 2) * 100)) / 100)

                'Scaled values should be used when precision counts
                ' Visual basic does not have a decimal value type
                ' but it does have a CDec (Convert to Decimal) operator
                Debug.Print "Decimal " & (Int(CDec(SomeN umberA) * 100)) / 100 & " is a " &
                TypeName((Int(C Dec(SomeNumberA ) * 100)) / 100)

                ' Currency Value types are the next best thing and will maintain precision
                ' notice this becomes a double again after all the operations are done
                Debug.Print "Currency " & (Int(SomeNumber B * 100)) / 100 & " is a " &
                TypeName((Int(S omeNumberB * 100)) / 100)

                ' A quick work around to your delima
                Debug.Print "Rounded Decimal " & Round(CDec(Some NumberA), 2) & " is a " &
                TypeName(Round( CDec(SomeNumber A), 2))

                ' Another way to skin the cat
                Debug.Print "Special " & SpecialReduce(S omeNumberA, 2) & " is a " &
                TypeName(Specia lReduce(SomeNum berA, 2))

                End Sub

                '
                Public Function SpecialReduce(I nputValue As Variant, Places As Integer) As
                Currency

                If Not IsNumeric(Input Value) Then
                Err.Raise 13, "My Module", "InputValue must be of numeric type."
                End If

                SpecialReduce = Round(CCur(Inpu tValue), Places)

                End Function





                "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                news:u8e4Gz4tFH A.3720@TK2MSFTN GP14.phx.gbl...[color=blue]
                > "Andrew Taylor" <andrew.taylor@ cantab.net> schrieb:[color=green]
                >> As always with this type of problem, the answer is that you can't rely
                >> on floating point numbers to give exact results.[/color]
                >
                > Additional information:
                >
                > <URL:http://groups.google.d e/group/microsoft.publi c.dotnet.langua ges.vb/msg/1d103c859080279 2>
                >
                > --
                > M S Herfried K. Wagner
                > M V P <URL:http://dotnet.mvps.org/>
                > V B <URL:http://classicvb.org/petition/>[/color]


                Comment

                • AMDRIT

                  #9
                  Re: VB 6.3 Int Problem

                  Here is some more explaination that doesn't require deep thought

                  Option Explicit

                  Sub SimpleTest()

                  Dim SomeNumberA As Double
                  Dim SomeNumberB As Currency
                  Dim SomeNumberC As Single

                  SomeNumberA = 8.950568
                  SomeNumberB = 8.950568
                  SomeNumberC = 8.950568

                  'Floating points are only usefull for scientific notation (Single and
                  Double)
                  Debug.Print "Single " & (Int(SomeNumber C * 100)) / 100 & " is a " &
                  TypeName((Int(S omeNumberC * 100)) / 100)

                  Debug.Print "Double " & (Int(SomeNumber A * 100)) / 100 & " is a " &
                  TypeName((Int(S omeNumberA * 100)) / 100)

                  Debug.Print "Rounded Double " & (Int(Round(Some NumberA, 2) * 100)) / 100 & "
                  is a " & TypeName((Int(R ound(SomeNumber A, 2) * 100)) / 100)

                  'Scaled values should be used when precision counts
                  ' Visual basic does not have a decimal value type
                  ' but it does have a CDec (Convert to Decimal) operator
                  Debug.Print "Decimal " & (Int(CDec(SomeN umberA) * 100)) / 100 & " is a " &
                  TypeName((Int(C Dec(SomeNumberA ) * 100)) / 100)

                  ' Currency Value types are the next best thing and will maintain precision
                  ' notice this becomes a double again after all the operations are done
                  Debug.Print "Currency " & (Int(SomeNumber B * 100)) / 100 & " is a " &
                  TypeName((Int(S omeNumberB * 100)) / 100)

                  ' A quick work around to your delima
                  Debug.Print "Rounded Decimal " & Round(CDec(Some NumberA), 2) & " is a " &
                  TypeName(Round( CDec(SomeNumber A), 2))

                  ' Another way to skin the cat
                  Debug.Print "Special " & SpecialReduce(S omeNumberA, 2) & " is a " &
                  TypeName(Specia lReduce(SomeNum berA, 2))

                  End Sub

                  '
                  Public Function SpecialReduce(I nputValue As Variant, Places As Integer) As
                  Currency

                  If Not IsNumeric(Input Value) Then
                  Err.Raise 13, "My Module", "InputValue must be of numeric type."
                  End If

                  SpecialReduce = Round(CCur(Inpu tValue), Places)

                  End Function





                  "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                  news:u8e4Gz4tFH A.3720@TK2MSFTN GP14.phx.gbl...[color=blue]
                  > "Andrew Taylor" <andrew.taylor@ cantab.net> schrieb:[color=green]
                  >> As always with this type of problem, the answer is that you can't rely
                  >> on floating point numbers to give exact results.[/color]
                  >
                  > Additional information:
                  >
                  > <URL:http://groups.google.d e/group/microsoft.publi c.dotnet.langua ges.vb/msg/1d103c859080279 2>
                  >
                  > --
                  > M S Herfried K. Wagner
                  > M V P <URL:http://dotnet.mvps.org/>
                  > V B <URL:http://classicvb.org/petition/>[/color]


                  Comment

                  Working...