Page::calculate($formula[,$decimals])

Version 3.8

Calculate given formula

Calculate given formula and return result. Fieldnames are replaced with fields Field::getNumber() -value.

Note about field order

Note about field order Calculation does not work, if calculation has a fieldname that contains a part of other fieldname which is after it.

Example

Test values
price = 10
price_multiplier = 1.2.

Counts wrong:

'price * price_multiplier'  --> '10*10_multiplier' --> '100'

Counts correctly:

'price_multiplier * price'  --> '1.2*10' --> '12'

Return value

Float

Arguments

$formula
Calculation where field names are replaced with field values.
Subfield values can be referenced by: field.subfield.
Subrow sums can be referenced by: [fieldname: subfield]
$decimals
Number of decimals in return value
Default: 2

Full documentation

Count total from amount and price

$total $Order_row->calculate('amount * price');

Use subfield data: Count total from amount and product price.

$total = $Order_row->calculate('amount * product.price');

Use subrows sum:

$total = $Order->calculate('delivery_cost + [order_row: amount * product_price]');

Use of parenthesis:

$total = $Order->calculate('total * (100 - discount_percent) / 100 ');