Difference between revisions of "S2 Cookbook: Numbers"

From Dreamwidth Notes
Jump to: navigation, search
(int <=> string conversion examples)
Line 61: Line 61:
 
var int pick3 = rand($pick, $pick2);
 
var int pick3 = rand($pick, $pick2);
 
</syntaxhighlight>
 
</syntaxhighlight>
 
== Convert a string to an integer ==
 
  
 
== Convert an integer to a string ==
 
== Convert an integer to a string ==
  
 +
The print statement and some operators will implicitly convert integers to strings:
 +
 +
<syntaxhighlight lang="s2">
 +
# Example function that appends "th" to an integer
 +
function ordinal(int num) : string
 +
{
 +
    return $num+"th"; # here the + operator converts to string
 +
}
 +
 +
var int nine = 9;
 +
# 9
 +
print "$nine"; # print $nine; works as well
 +
# 9th
 +
print ordinal($nine);
 +
</syntaxhighlight>
 +
 +
However, if you find you need to explicitly convert, you can use the string() function:
 +
 +
<syntaxhighlight lang="s2">
 +
var string nine = string(9);
 +
# 9
 +
print "$nine";
 +
</syntaxhighlight>
 +
 +
== Convert a string to an integer ==
 +
 +
To convert the other way, you will probably need the int() function:
 +
<syntaxhighlight lang="s2">
 +
var int ten = int("10");
 +
var int sum = ten + 9;
 +
# 19
 +
print "$sum";
 +
# This will yield the error "Can't initialize variable 'ten' of type int with expression of type string"
 +
var int ten = 1 + "9";
 +
</syntaxhighlight>
 
[[Category: S2 Cookbook]]
 
[[Category: S2 Cookbook]]

Revision as of 20:58, 16 August 2010

Arithmetic

Comparing two numbers to see if they are equal or one is lesser/greater

Seeing if a number is odd or even

The mod operation, signified by the % symbol, can tell you if a number is odd or even. Mod a number by 2, and if the result is equal to 1, the number is odd. If the result is equal to 0, the number is even.

var int odd = 8;
 
# prints "Odd"
if( $odd % 2 == 1 ) {
    print "Odd";
} else {
    print "Even";
}
 
var int even = 6;
 
# prints "Even"
if( $even % 2 == 0 ) {
    print "Even";
} else {
    print "Odd";
}

Pad a number with 0s to a certain number of digits

Use the zeropad function to get a string that's padded with 0s to a certain number of digits:

var int label = 1;
# 001
print zeropad($label, 3) + "<br />";
 
# works on strings that are integers, too!
var string fake_int = "2";
# 0002
print zeropad($fake_int, 4) + "<br />";
 
# this won't work because "A2" won't convert to an integer correctly
var string fake_label = "A2";
# 000
print zeropad($fake_label, 3) + "<br />";

Get a random number

You can create random numbers using the two kinds of rand functions:

# returns a random number between 1 and 10
var int pick = rand(10);
# returns a random number between 11 and 20
var int pick2 = rand(11, 20);
# returns a random number between the two random numbers
var int pick3 = rand($pick, $pick2);

Convert an integer to a string

The print statement and some operators will implicitly convert integers to strings:

# Example function that appends "th" to an integer
function ordinal(int num) : string
{
    return $num+"th"; # here the + operator converts to string
}
 
var int nine = 9;
# 9
print "$nine"; # print $nine; works as well
# 9th
print ordinal($nine);

However, if you find you need to explicitly convert, you can use the string() function:

var string nine = string(9);
# 9
print "$nine";

Convert a string to an integer

To convert the other way, you will probably need the int() function:

var int ten = int("10");
var int sum = ten + 9;
# 19
print "$sum";
# This will yield the error "Can't initialize variable 'ten' of type int with expression of type string"
var int ten = 1 + "9";