Difference between revisions of "Help:TT Arithmetic Operators"
HELP PAGE DISCUSSION CLOSE
m
 
m (1 revision)
 
(No difference)

Latest revision as of 23:21, 24 March 2013

Performing math operations on variables

In the previous chapters we have covered variable creation, storing, and retrieving. We will take a step further and apply mathematics to the variables. Look over some of the available operators, and get an idea what they do.

SYMBOL  DEFINITION       EXAMPLE   RESULT
  +     addition         5 + 2     7
  -     subtraction      3 - 6     -3
  *     multiplication   2 * 2.5   5
  /     division         9 / 2     4.5
  %     remainder        12 % 5    2

Most of the operators above are pretty self-explanatory, but the remainder operator might be new to some of you. The number in front of the remainder symbol % will be divided by the number trailing the operator. The remainder of the division will be returned. So in the example of 12 % 5, 5 goes into 12 twice, so 2 remains after the division. TT can accept negative numbers and floating point numbers (decimal numbers). Onto to some examples of using arithmetic operators with variables.

[% hats = 5; costperhat = 12.95; totalcost = hats * costperhat; "We have ${hats} hats.\n"; "Each hat costs \$${costperhat} dollars.\n"; "The total cost for the hats is \$${totalcost}.\n"; %]

The example above will result in the output below:

We have 5 hats. Each hat costs $12.95 dollars. The total cost for the hats is $64.75.

The code above simply creates a few variables, performs some arithmetic operations with the variables, then prints out the result. Note that to print out the dollar sign within double quote strings, you need to put a backslash character in front to avoid variable interpolation, otherwise TT will assume that it is the beginning of a variable.


Back to Table of Contents