TGC Codebase Backup



Rounding Function by Anonymous Coder

16th Nov 2006 11:55
Summary

Function for cleaning up float decimals



Description

This function returns a string from a decimal number, allowing float type decimals to be rounded off to any place. To use, type num$ = Round(number, places) the number can be anything, a decimal, a whole number, or even negative. The places can also be positive or negative, but cannot contain a decimal place. Positive numbers cause a decimal place rounding, such as round(1.2345,3) would return a string "1.235" Using a negative number allows rounding to the tens, hundreds, thousands, etc. ie round(12345,-3) returns "12000" The code is a bit long, I haven't gone through and tried to streamline it, but it does the job for returning cleaner numbers for display.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Function Round(Number as float, Places as integer)

     local rounder       as integer                    `this variable will be the one that determines whether to round up or down
     local IntGroup      as integer                    `this holds the INTEGER digits to the left of the decimal
     local DecGroup      as float                      `this holds the DECIMAL digits to the right of the decimal
     local RoundedNum    as string                     `this holds the finished product for return to the Round funky
     local Zeros         as integer                    `this holds the number of Zeros when rounding a large whole number
     local DecHolder     as integer                    `This holds the DB "weird" decimal for processing
     local DecString     as string                     `this holds the string of the decimal side of a rounded number
     local NegativeNum   as Boolean                    `This holds the info about whether a number is negative when recieved

     If Number < 0
          NegativeNum = 1                              `This sets a boolean to a a negative sign at the end of the function if
          Number = abs(Number)                         `the incoming number was negative and sets
     else NegativeNum = 0                              `the number to a real number
     endif

 `This is the handler for rounding to the nearest whole number, dropping the decimal
     if Places = 0
          IntGroup = int(Number)
          rounder = val(right$(left$(str$(Number),len(str$(IntGroup))+2),1))    `Rounder determines the digit used to round up or down
          if rounder => 5 then inc IntGroup
          RoundedNum = str$(IntGroup)
     endif
`This is the handler for rounding whole numbers to the nearest tens, hundreds, thousands etc.
     if Places < 0
          PlaceHolder = abs(Places)
          IntGroup = int(Number)
          rounder = val(left$(right$(str$(IntGroup), PlaceHolder),1))           `Rounder determines the digit used for rounding up or down
          if rounder => 5
                  IntGroup = IntGroup - val(right$(str$(IntGroup), PlaceHolder))
                    Zeros = 1                                                   `This creates a number based on the place to be rounded
                    for x = 1 to PlaceHolder
                         Zeros = Zeros * 10
                    next x
                  IntGroup = IntGroup / Zeros                                   `And here we use that number to increment the prper number
                  inc IntGroup
                  IntGroup = IntGroup * Zeros
             else
                  IntGroup = IntGroup - val(right$(str$(IntGroup), PlaceHolder))`Here we simply subtract the rightMost digits, making them Zeros
          endif
          RoundedNum = str$(IntGroup)
     endif
`This is the handler for rounding to the nearst decimal place.  For Accuracy, rounder will run two cycles because of DB decimal problem
     if Places > 0
          IntGroup = int(Number)
          DecGroup = Number - IntGroup                                               `SETUP THE DECIMAL WORKS

               if DecGroup = 0
                    RoundedNum = str$(IntGroup)                                           `checks to see if there is nothing
                    goto skip                                                        `to round, returns a whole number
               endif

          rounder   = val(Right$(left$(str$(DecGroup), Places + 4),1))
          DecHolder = val(Right$(left$(str$(DecGroup), Places + 3),1))
          DecString = right$(left$(Str$(DecGroup), Places + 2), Places + 1)
               if rounder => 5                                                       `ROUND ONE OF NEGOTIATING THE ROUNDING
                    Inc DecHolder
                    DecString = DecString + Str$(DecHolder)                          `Round up
               else DecString = DecString + Str$(DecHolder)                          `Round Down
               endif
          rounder = val(Right$(DecString, 1))                                        `Grab last digit
          DecHolder = val(left$(right$(DecString, 2),1))                             `grab second to last digit
               If rounder => 5                                                       `ROUND TWO of the rounding
                    inc DecHolder
                    DecString = left$(DecString , Places ) + str$(DecHolder)
               else DecString = left$(DecString , Places ) + str$(DecHolder)
               endif
          RoundedNum = Str$(IntGroup) + DecString
     endif

          Skip:                                                                      `allows escape from the decimal rounding if
          if NegativeNum = 1 then RoundedNum = "-" + RoundedNum                      `If Negative, then add a negative sign

endfunction RoundedNum