There are some usefull functions missing from SC, in my opinion. This is the place to share those functions (aka making a method that does the same as the function)
Floor(number)Function: always rounds down the input number. So 10.1 becomes 10, but 10.9 also becomes 10.
name: floor
parameters: number
return type: integer
LET string$ = STR$(number)
LET string$ = NTHFIELD$(string$,".",1)
LET number = VAL(string$)
RETURN number
Explanation:
first convert the integer to a string. Use that string to seperate the numbers infront and after the "." from each other. Then convert the first number back to a string. So in other words, it cuts of everything after the ".".
Example:
LET number = 5.2
LET number = floor(number)
PRINT STR$(number)
Prints 5
Ceil(number)Function: always rounds up the input number. So 10.9 becomes 11, but 10.1 also becomes 11.
name: ceil
parameters: number
return type: integer
LET string$ = STR$(number)
LET string1$ = NTHFIELD$(string$,".",1)
LET string2$ = NTHFIELD$(string$,".",2)
IF string2$ <> "" then
 LET number = VAL(string1$)
 LET number = number + 1
END IF
RETURN number
Explanation:
First convert the number to a string. Story the number infront of the "." in string1$ and everything after in string2$. Then check if string2$ contains any data. If you won't do this check, it will screw up (example: 10 becomes 11). Converrt string1$ (so the number infront of the ".") back to a number so you can add one up (the rounding up part). Then return the number.
Example
LET number = 5.2
LET number = floor(number)
PRINT STR$(number)
Prints 6
12to24$(time$)This is somewhat more advanced. If you to add real time features like day and night in your game, you might get some bugs when your game is played in different parts of the world. This method converts 12-hour-clock time to 24-hour-clock time.
name: 12to24$
parameters: time$
return type: string
LET string1$ = NTHFIELD$(time$," ",1)
LET string2$ = NTHFIELD$(time$," ",2)
IF string2$ = "pm" then
 LET hours$ = NTHFIELD$(string1$,":",1)
 LET hours = VAL(hours$)
 LET hours = hours + 12
IF hours => 24 then
LET hours = hours - 24
END IF
 LET newhours$ = STR$(hours)
 LET string1$ = REPLACE$(string1$,hours$,newhours$)
END IF
RETURN string1$
Explanation:
First seperate the time from the "am" or "pm". Check if string2$ is pm, if so add 12 hours to the hours. Replace the new hour with the old and output.
example:
LET time$ = 12to24$("2:30 am")
PRINT time$
This prints 2:30
LET time$ = 12to24$("2:30 pm")
PRINT time$
This prints 14:30
24to12$(time$)This coverts 24-hour time to 12-hour time (including the addfix pm or am).
name: 24to12$
parameters: time$
return type: string
LET string1$ = NTHFIELD$(time$," ",1)
LET string2$ = NTHFIELD$(time$," ",2)
if string2$ = "" then
LET hours$ = NTHFIELD$(string1$,":",1)
LET hours = VAL(hours$)
if hours > 12 then
LET hours = hours - 12
let add$ = " pm"
else
let add$ = " am"
end if
LET newhours$ = STR$(hours)
LET string1$ = REPLACE$(string1$,hours$,newhours$)
end if
return string1$+add$
So basicly this first checks if there isn't an addfix like pm or am. Then it checks if the time is after 12 noon, if so substract 12 and add pm. If not, just add am. And output this.
example:
LET time$ = 24to12$("13:15")
PRINT time$
Prints 1:15 pm
LET time$ = 24to12$("1:15")
PRINT time$
Prints 1:15 am
Note that it outputs 12:00 noon as 12:00 pm, and 12:00 midnight as 12:00 pm. You might want to build in some kind of check if you want to change this.
So with the last two methods you just convert the time, to make sure it's in your prefered format, and then just do whatever you want to do with it.
Sare your methods, new functions are hacks.