Difference between revisions of "S2 Cookbook: Numbers"
From Dreamwidth Notes
(int <=> string conversion examples) |
Foxfirefey (Talk | contribs) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | Unfortunately, the only numbers available in S2 are integers. | ||
+ | |||
+ | == Declaring an integer variable == | ||
+ | |||
+ | <syntaxhighlight lang="s2"> | ||
+ | var int b = 6; | ||
+ | </syntaxhighlight> | ||
+ | |||
== Arithmetic == | == Arithmetic == | ||
+ | |||
+ | <syntaxhighlight lang="s2"> | ||
+ | var int a = 8; | ||
+ | var int b = 6; | ||
+ | var int c = 3; | ||
+ | |||
+ | # 14 | ||
+ | print $a + $b; | ||
+ | print "\n"; | ||
+ | |||
+ | # -2 | ||
+ | print $b - $a; | ||
+ | print "\n"; | ||
+ | |||
+ | # 2 | ||
+ | print $a / $c; | ||
+ | print "\n"; | ||
+ | |||
+ | # 18 | ||
+ | print $b * $c; | ||
+ | print "\n"; | ||
+ | </syntaxhighlight> | ||
+ | |||
== Comparing two numbers to see if they are equal or one is lesser/greater == | == Comparing two numbers to see if they are equal or one is lesser/greater == | ||
+ | <syntaxhighlight lang="s2"> | ||
+ | var int a = 8; | ||
+ | var int b = 6; | ||
+ | var int c = 6; | ||
+ | # prints "Equal" | ||
+ | # if $b is equal to $c | ||
+ | if( $b == $c ) { | ||
+ | print "Equal"; | ||
+ | } else { | ||
+ | print "Not equal"; | ||
+ | } | ||
+ | |||
+ | # prints "Less" -- ie, $a is less than $b | ||
+ | if( $a < $b ) { | ||
+ | print "Less"; | ||
+ | # $a is greater than $b | ||
+ | } elseif( $a > $b ) { | ||
+ | print "Greater"; | ||
+ | } else { | ||
+ | print "Equal"; | ||
+ | } | ||
+ | |||
+ | # prints "Positive | ||
+ | # if a is less than or equal to 0 | ||
+ | if( $a <= 0 ) { | ||
+ | print "Zero or negative"; | ||
+ | } else { | ||
+ | print "Positive"; | ||
+ | } | ||
+ | |||
+ | # prints "Not so big." | ||
+ | # if a is equal to 100 or greater | ||
+ | if( $a >= 100 ) { | ||
+ | print "Big!"; | ||
+ | } else { | ||
+ | print "Not so big."; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
== Seeing if a number is odd or even == | == Seeing if a number is odd or even == | ||
Line 99: | Line 168: | ||
var int ten = 1 + "9"; | var int ten = 1 + "9"; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
[[Category: S2 Cookbook]] | [[Category: S2 Cookbook]] |
Latest revision as of 05:48, 12 June 2011
Unfortunately, the only numbers available in S2 are integers.
Contents
Declaring an integer variable
var int b = 6;
Arithmetic
var int a = 8; var int b = 6; var int c = 3; # 14 print $a + $b; print "\n"; # -2 print $b - $a; print "\n"; # 2 print $a / $c; print "\n"; # 18 print $b * $c; print "\n";
Comparing two numbers to see if they are equal or one is lesser/greater
var int a = 8; var int b = 6; var int c = 6; # prints "Equal" # if $b is equal to $c if( $b == $c ) { print "Equal"; } else { print "Not equal"; } # prints "Less" -- ie, $a is less than $b if( $a < $b ) { print "Less"; # $a is greater than $b } elseif( $a > $b ) { print "Greater"; } else { print "Equal"; } # prints "Positive # if a is less than or equal to 0 if( $a <= 0 ) { print "Zero or negative"; } else { print "Positive"; } # prints "Not so big." # if a is equal to 100 or greater if( $a >= 100 ) { print "Big!"; } else { print "Not so big."; }
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";