Great Ocean Media
website design - editorial & pr services - freelance journalism

adi_calc – Calculator for TXP variables

This plugin does calculations using TXP variables.

Usage

adi_calc may be used as a single or container tag.

Attributes

name="var name"

- a TXP variable to work with. This attribute is optional & is ignored in container mode. If not supplied in single tag mode then the initial value for the calculation is taken from the value attribute. If this is not supplied, then zero is used.

value="value"

- the initial value used for the calculation. This attribute is optional. Default = “” (i.e. if name variable exists, use its value; else use zero). value will override name if both are supplied.

display="boolean"

- display the variable’s value after the calculation has been performed. Default = “0” (do not display). If neither name or result attributes are supplied for the calculation then display is automatically defaulted to “1”.

add="value"

- add “value”.

subtract="value"

- subtract “value”.

multiply="value"

- multiply by “value”.

div="value"
divide="value"

- divide by “value”. By default, this is integer division & will always round down, e.g. 11 DIV 5 = 2. See precision for enabling floating point calculations (& hence rounding up or down).

mod="value"

- divide by “value” & return the remainder.

ceiling="boolean"

- used with div to round up the result, e.g. 11 DIV 5 = 3. Default = “0” (don’t round up).

precision="value"

- switch from integer to floating point calculation and, optionally, specify the precision in decimal places. Results rounded up or down accordingly. Default = not defined (integer calculation). To switch on floating point mode without specifying the number of decimal places, use precision="".

result="var name"

- a TXP variable to contain the result of the calculation. If used then the original variable, specified by name left unchanged. Default = “”.

reset="value"

- the point at which the variable specified by name is reset to zero. Default = “” (no reset).

reset_name="var name"

- a TXP variable to count resets. Default = “” (no reset counter)

Notes

Examples

Single tag mode

<txp:variable name="test" value="2" />
set a variable called test to 2

<txp:adi_calc name="test" add="5" />
adds 5 to test (result = 7)

<txp:adi_calc name="test" subtract="3" />
subtracts 3 from test (result = 4)

<txp:adi_calc name="test" multiply="9" />
multiplies test by 9 (result = 36)

<txp:adi_calc name="test" div="5" />
is integer division by 5 (result = 7)

<txp:adi_calc name="test" mod="2" />
is the remainder when divided by 2 (result = 1)

In the above sequence of calculations the setting of the variable and the first calculation can be combined using the value attribute:

<txp:adi_calc name="test" value="2" add="5" />
sets test to 2 and adds 5 to it (result = 7)

Use display="1" to automatically display the result of the calculation:

There are <txp:adi_calc name="total" value="60" multiply="24" display="1" /> minutes in a day.

And TXP variable total will contain the result, i.e. 1440.

Container tag mode

<txp:adi_calc name="x" add="2">4</txp:adi_calc>
contents (i.e. 4) override initial value of supplied variable x, but the result will be stored in x

<txp:adi_calc display="1" add="2" result="y">
<txp:variable name="x"/>
</txp:adi_calc>
initial value taken from variable x, 2 added, result stored in y and displayed, x is unchanged

<txp:adi_calc name="x" add="2"></txp:adi_calc>
contents of container empty, initial value taken from variable x, 2 added

Floating point calculations

<txp:adi_calc name="float" value="1" div="3" precision="" />
divides 1 by 3 (result = 0.33333333333)

<txp:adi_calc name="float" value="2" div="3" precision="2" />
2 divided by 3, two decimal places (result = 0.67)

Well rounded

<txp:adi_calc name="round_me_down" />
will round down the value in the supplied variable

<txp:variable name="pi" value="3.14159265" />
<txp:adi_calc name="pi" precision="2" />
will round pi to 2 decimal places

No variables required

There are <txp:adi_calc value="60" multiply="24" /> minutes in a day.

<txp:adi_calc add="4">20</txp:adi_calc> is two dozen.

pi is roughly <txp:adi_calc value="22" divide="7" precision="2" />

Using reset & reset counter

Consider the following:

<txp:variable name="counter" value="0" />
<txp:variable name="reset_counter" value="0" />
counter = 0, reset_counter = 0

<txp:adi_calc name="counter" add="1" reset="4" reset_name="reset_counter" />
counter = 1, reset_counter = 0

if the above adi_calc tag is repeated a further 6 times, then the sequence of values will be as follows:

counter = 2, reset_counter = 0
counter = 3, reset_counter = 0
counter = 0, reset_counter = 1
counter = 1, reset_counter = 1
counter = 2, reset_counter = 1
counter = 3, reset_counter = 1

Each time counter reaches the value of 4, it is reset to zero and the reset counter is incremented. This can be useful when counting rows & columns. In the above example, reset_counter would be row and counter would be column.

Additional information

Support and further information can be obtained from the Textpattern support forum. A copy of this help is also available online. More adi_plugins can be found here.

Version 1.2Download (Uncompressed) SupportTXP 4.5, 4.6