|
Variables - Variable Related Functions |
Top Previous Next |
|
Travelling ? It's a common practice to learn basics of foreign language to make your visit more comfortable. You can, of course, pay translator to do the job. Doing educational courses ? You'll give your best to translate knowledge to acceptable form, comprehendable by the course group. Using computer ? By time you've read this paragraph, it performed millions of translations. We're obviously talking about MMB, using some translations in script language too ! In sections above we have mentioned string and numerical variables with all their differences. Using of variable-related functions makes variable differences disappear.
And to achieve that goal, we'll need a kind of translation. In MMB case, we'll perform translations between numerical and string variables. CHAR(NumVariable) converts numerical variable to a string variable Specifying of numerical variable we want to convert to string is done by using source numerical variable label inside parenthesis:
CHAR(NumOfYears) This would hardly make sense without specifying destination string variable, where converted numerical variable will be put. So we also need one string variable for the left side of code line: Years$ Put together, code line for numerical -> string conversion looks like this: Years$ =CHAR(NumOfYears)
Script line above will convert content of NumOfYears numerical variable to string and assign result to string variable Years$ . This type of conversion (numbers to text) is used most often. Here's an example of displaying advanced string with data retrieved from numerical variable: NumOfYears=36 UserAge$= CHAR(NumOfYears)+' years old' Message("Our user is ","UserAge$") Result of these code lines will be displayed in message box: Our user is 36 years old From this example is visible for what you'll use CHAR conversion - when you want to display numbers from numerical variable in string / text content.
VAL(StringVariable$) converts string variable to numerical variable Specifying of string variable we want to convert to number is done by using source string variable label inside parenthesis:
VAL(Year$) This function requires specifying of destination numerical variable, where converted string variable will be put. So we also need one numerical variable for the left side of code line: Year Put together, code line for string -> number conversion looks like this: Year =VAL(Year$)
Script line above will convert content of Year$ string variable to a number and assign result to numerical variable Year . Here's an example of using VAL function to get year, perform math-related operation and display result in the message box: Year$='2003' Year=VAL(Year$) Year=Year+10 Message("For ten years it will be: ","Year") Result of this code lines will be displayed in message box: For ten years it will be: 2013 From this example is visible for what you'll use VAL conversion - when you want to perform math operations on some number that was previously a string (text).
INT(NumVariable) rounds floating point number to lower integer value Specifying of numerical variable we want to round value for is done using source numerical variable label inside parenthesis:
INT(CarTax) This function requires specifying destination numerical variable, where integer number will be put. So we also need one numerical variable for the left side of code line: RoundCarTax Put together, code line for floating point -> integer conversion looks like this: RoundCarTax =INT(CarTax)
Script line above will round floating point content of CarTax numerical variable to integer and assign result to numerical variable RoundCarTax . Here's an example of using INT function in practice: CarTax=14.32 RoundCarTax=INT(CarTax) Message("Rounded tax for car is: ","RoundCarTax") Result of this code lines will be displayed in the message box: Rounded tax for car is: 14 Notice how variable is rounded to lower value. This is being done in all cases: CarTax=14.99 RoundCarTax=INT(CarTax) Message("Rounded tax for car is: ","RoundCarTax") Result of INT function in this case will not be rounded to a higher value: Rounded tax for car is: 14 INT function is in MMB mostly used for percentage counting (progress bars, gauges).
ABS(NumVariable) rounds floating point number to a lower integer & absolute value What "absolute" means ? Number can't be negative. If numerical variable content is lower than zero, negative prefix is removed and number becomes positive (above zero). Specifying numerical variable we want to round & absolute value for is performed using source numerical variable label inside parenthesis:
ABS(CupsOfCoffee) This function requires specifying destination numerical variable, where round & absolute number will be put. So we also need one numerical variable for the left side of the code line: CoffeeCups Put together, code line for real number -> absolute number conversion looks like this: CoffeeCups =ABS(CupsOfCoffee)
Script line above will (optionally) round floating point content of CupsOfCoffee numerical variable to integer, check if number is negative so it can be converted to positive value and assign result to the numerical variable CoffeeCups . Here's an example of using ABS function in practice: CupsOfCoffee=-5.5 CoffeeCups=ABS(CupsOfCoffee) Message("Number of coffee cups to order: ","CoffeeCups") Result of these code lines will be displayed in message box: Number of coffee cups to order: 5 ABS function is mostly used for prevention of negative numerical values.
RND(NumVariable) generates random number in the range specified using numerical variable This is specific variable-related function. It does not convert input variable, but uses it as a range limiter when generating random numbers. Specifying numerical variable that is upper limit for random number range is done by using source numerical variable label or fixed number inside parenthesis:
RND(UpLimit) ...or... RND(48) This function requires specifying destination numerical variable, where generated random number will be put. So we also need one numerical variable for the left side of a code line: LottoNumber
Put together, code line for random number generator looks like this: LottoNumber =RND(UpLimit) ...or... LottoNumber =RND(48)
Script lines above will use content of numerical variable (UpLimit) or fixed number (48) as upper limit of generated random number and assign result to numerical variable LottoNumber . Generated number will be within range 0 - upper limit . Here's an example of using RND function in practice: LottoNumber=RND(48) Message("Today's lucky number is: ","LottoNumber") Result of this code lines will display message box text... Today's lucky number is: ...and append random number (RND function result) to it. Calling RND function repeatedly will generate numbers that differ from previous ones. Of course, having limited range, numbers will eventually repeat.
|