Weird rounding issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Leanne
    Mega Mivite






    • Mar 2006
    • 2666

    #1

    Weird rounding issue

    It was my understanding that Miva uses bankers rounding, where numbers ending in 5 will round to the nearest even number. So, 0.125 rounds to 0.12 and 0.135 rounds to 0.14. However, I am not getting those rounding results.

    Code:
        <mvt:assign name="g.discount_raw" value="109.25* .18" />
        <mvt:assign name="g.discount_rounded" value="rnd(g.discount_raw,2)" />
        <p>    
            $109.25 x .18 = &mvt:global:discount_raw; <br>
            &mvt:global:discount_raw rounded is &mvt:global:discount_rounded;
        </p>
    Resulting output is consistent with bankers rounding:

    $109.25 x .18 = 19.665
    19.665 rounded is 19.66


    Code:
        <mvt:assign name="g.discount_raw" value="84.75 * .18" />
        <mvt:assign name="g.discount_rounded" value="rnd(g.discount_raw,2)" />
        <p>    
            $84.75 x .18 = &mvt:global:discount_raw; <br>
            &mvt:global:discount_raw rounded is &mvt:global:discount_rounded;
        </p>
    Resulting output is not consistent with bankers rounding (or at least not my understanding of it):

    $84.75 x .18 = 15.255
    15.255 rounded is 15.25

    Is my understanding of Miva's rounding system incorrect? This makes it seem like numbers ending in 5 just round down?
  • Bruce - PhosphorMedia
    Developer Partner











    • Mar 2006
    • 11257

    #2
    I can't find it right now, but I believe there is a settings somewhere to use or not use 'Bankers' rounding. If that's not checked, it may effect what you are seeing.
    Bruce Golub
    Phosphor Media - "Your Success is our Business"

    Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
    phosphormedia.com

    Comment

    • Leanne
      Mega Mivite






      • Mar 2006
      • 2666

      #3
      You are correct, there is, (under Store Settings, I believe) but it is specific to sales tax.

      Comment

      • Bruce - PhosphorMedia
        Developer Partner











        • Mar 2006
        • 11257

        #4
        Yea, I don't think the general ROUND function uses bankers, that's why they had to add that to sales tax. Here's what they do in the sales tax module

        Code:
        <MvFUNCTION NAME = "StateTax_Round" PARAMETERS = "charge, round_meth" STANDARDOUTPUTLEVEL = "">
            <MvCOMMENT> Bankers' rounding: Round down on 4 or less, stay on 5, round up on 6 or more </MvCOMMENT>
        
            <MvIF EXPR = "{ l.round_meth EQ 'B' }">
                <MvFUNCTIONRETURN VALUE = "{ l.charge ROUND 2 }">
            </MvIF>
        
            <MvCOMMENT> Rounding up: Round up on 5 or more </MvCOMMENT>
        
            <MvASSIGN NAME = "l.pos" VALUE = "{ '.' IN l.charge }">
        
            <MvIF EXPR = "{ ( l.pos EQ 0 ) OR ( substring_var( l.charge, l.pos + 3, 1 ) NE '5' ) }">
                <MvFUNCTIONRETURN VALUE = "{ l.charge ROUND 2 }">
            </MvIF>
        
            <MvFUNCTIONRETURN VALUE = "{ ( l.charge + 0.001 ) ROUND 2 }">
        </MvFUNCTION>
        You'd have to adopt that to SMT code. (B is bankers :) )
        Bruce Golub
        Phosphor Media - "Your Success is our Business"

        Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
        phosphormedia.com

        Comment

        • Leanne
          Mega Mivite






          • Mar 2006
          • 2666

          #5
          Thank you! This comment at the beginning of the code sample looks like it might explain my results. I'll do some more experimenting and see if that is actually what is happening.

          Code:
           
           <MvCOMMENT> Bankers' rounding: Round down on 4 or less, [B][COLOR=#FF0000]stay on 5[/COLOR][/B], round up on 6 or more </MvCOMMENT>

          Comment

          • Bruce - PhosphorMedia
            Developer Partner











            • Mar 2006
            • 11257

            #6
            You can sometimes google math questions like this for a php based solution. Script Maths are pretty universal and relatively easy to convert.
            Bruce Golub
            Phosphor Media - "Your Success is our Business"

            Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
            phosphormedia.com

            Comment

            Working...
            X