Tuesday, June 1, 2010

Number Rounding in SCL

I recently found the need to do some simple number rounding in SCL, and was able to dig these two routines out using my SCL Subroutine Indexer though the index was from a previous computer I used, and hadn't copied over to my current machine. Thankfully, Google Desktop was able to find a backup copy I had made who knows when. So, to prevent me from losing these simple functions forever, here they are.

Nothing fancy; I round a number down by converting it to an integer string, then reading it back to a number using VAL. I guess SCL will truncate a number at the decimal point when converting to an integer string (%g), rather than do any real rounding. So to round up I just round down and add one. Like I said, nothing fancy, and apparently nothing worth searching for 20 minutes over. Anyways, these would be good to keep compiled all the time.

routine round_down_to_int( num_value : Real ) : Integer
Var

int_string : String
result : Integer

Begin

result = 1
int_string = str( '%g' , num_value )
result = VAL( int_string )
return result

End
routine round_up_to_int( num_value : Real ) : Integer
Var

result : Integer
rounded_down : Integer

Begin

return round_down_to_int( num_value ) + 1

End

No comments: