Rounding Problems Calculating Sales Tax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #16
    Originally posted by NeoPa
    I don't believe there is a solution as such. We are dealing with an arithmetic approximation which means that recognising an exact figure (anything.??5) is not always possible.

    Actually, an actual solution could be managed by using different variable types, but would be a bit messy. Smiley's proposed solution is probably the tidier, as it doesn't assume the result is low, but either would be practically workable.

    As Stewart says, a decimal variable would handle the situation, or another more kludgy way would be an integer (or Long) variable multiplied, then rounded, then divided to handle the decimal places.

    Anyway, it was a good spot by Stewart :)
    This could be one of your Kludgies! (LOL)
    Code:
    Public Function MyRound(varValue As Variant, intDecimalPlaces As Integer)
      MyRound = Int((varValue * intDecimalPlaces) + 0.5) / intDecimalPlaces
    End Function
    '
    Code:
    'Round to 2 Decimal Places
    Debug.Print MyRound(1.055,100) produces ==> 1.06

    Comment

    • loonette
      New Member
      • Apr 2010
      • 17

      #17
      Thanks a million guys, I used Smiley's formula in post 13 and it worked! I didn't realize I was going to start such a debate, but I really appreciate all of your effort to help me fix this problem. Take care!

      Comment

      • TheSmileyCoder
        Recognized Expert Moderator Top Contributor
        • Dec 2009
        • 2322

        #18
        I have given this some more though, and realise that the suggestion I gave will stop working for larger numbers. I therefore amend it to:
        Round(Round(Fin alPrice*TaxRate ),3),2)

        Now it is only basing the rounding in the signaficant figures (unless you exceed 1 million in cost, in which case I doubt anyone will give a flying rats arse about whether the rounding of the last cent was off :)

        I can't immediatly think of any place where this might fail. Finally Stewart, im impressed you caught this one :)

        Comment

        • Stewart Ross
          Recognized Expert Moderator Specialist
          • Feb 2008
          • 2545

          #19
          (Blush) Thanks Smiley!

          I'm glad that Loonette's problem is now resolved, and with the simple double rounding you've provided rather elegantly too.

          -S

          Comment

          Working...