Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

BNRound - Not Banker's Round

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • BNRound - Not Banker's Round

    Code:
    'BNRound - Not Banker's Rounding
    'By Colin Schmidt, [email protected], released to public domain, use at own risk
     
    FUNCTION NBRound(BYVAL pxVal AS EXT, BYVAL plDec AS LONG) AS EXT
        FUNCTION = (CQUD(((ABS(pxVal) * (10 ^ plDec)) * 2) + 0.5) \ 2) / (10 ^ plDec) * SGN(pxVal)
    END FUNCTION
     
    FUNCTION PBMAIN
        ?   FORMAT$(NBRound(-03.502, 0)) + $CR + _
            FORMAT$(NBRound(-03.501, 0)) + $CR + _
            FORMAT$(NBRound(-03.500, 0)) + $CR + _
            FORMAT$(NBRound(-03.499, 0)) + $CR + _
            FORMAT$(NBRound(-03.498, 0)) + $CR + _
            $CR+ _
            FORMAT$(NBRound( 03.502, 0)) + $CR + _
            FORMAT$(NBRound( 03.501, 0)) + $CR + _
            FORMAT$(NBRound( 03.500, 0)) + $CR + _
            FORMAT$(NBRound( 03.499, 0)) + $CR + _
            FORMAT$(NBRound( 03.498, 0)) + $CR + _
            $CR+ _
            FORMAT$(NBRound(-01.252, 1)) + $CR + _
            FORMAT$(NBRound(-01.251, 1)) + $CR + _
            FORMAT$(NBRound(-01.250, 1)) + $CR + _
            FORMAT$(NBRound(-01.249, 1)) + $CR + _
            FORMAT$(NBRound(-01.248, 1)) + $CR + _
            $CR+ _
            FORMAT$(NBRound( 01.252, 1)) + $CR + _
            FORMAT$(NBRound( 01.251, 1)) + $CR + _
            FORMAT$(NBRound( 01.250, 1)) + $CR + _
            FORMAT$(NBRound( 01.249, 1)) + $CR + _
            FORMAT$(NBRound( 01.248, 1))
     
        LOCAL llCount1 AS LONG
        LOCAL lnTimer AS SINGLE
        LOCAL lxTest1, lxTest2 AS EXT
        lxTest2 = 3.456
     
        lnTimer = TIMER
        FOR llCount1 = 999999 TO 0 STEP -1 '1 million ops = 0.35 seconds on 1.6GHz AMD laptop
            lxTest1 = NBRound(lxTest2, 2)
        NEXT llCount1
        lnTimer = TIMER - lnTimer
     
        ? FORMAT$(NBRound(lnTimer, 2)) + " seconds"
     
    END FUNCTION
Working...
X