Skip to main content
Math functions perform arithmetic and rounding operations on numeric field values. All variadic functions accept any mix of field names and literal numbers as arguments.

Reference

Fluent API methodFQL functionDescription
ceil($field)CEIL(field)Round a number up to the nearest integer
floor($field)FLOOR(field)Round a number down to the nearest integer
round($field, $decimals)ROUND(field, decimals)Round a number to the given decimal places
modulo($field, $divisor)MOD(field, divisor)Return the remainder of a division
add(...$operands)ADD(...)Add two or more numbers or fields
subtract(...$operands)SUB(...)Subtract operands from the first value
multiply(...$operands)MULTIPLY(...)Multiply two or more numbers or fields
divide(...$operands)DIVIDE(...)Divide the first value by subsequent operands

Examples

$query
    ->ceil('price')->as('priceCeil')
    ->floor('price')->as('priceFloor')
    ->round('price', 2)->as('priceRounded')
    ->modulo('quantity', 3)->as('remainder')
    ->add('price', 10, 'tax')->as('totalWithTax')
    ->subtract('price', 5, 'discount')->as('netPrice')
    ->multiply('price', 1.2, 'taxRate')->as('grossPrice')
    ->divide('totalRevenue', 'quantity', 1.2)->as('unitNetRevenue');

Variadic arithmetic functions

ADD, SUB, MULTIPLY, and DIVIDE accept a variable number of arguments. Each argument can be either a field name or a literal number. Field names are resolved against the current row; unresolved names fall back to being treated as literal values.
// All of the following are valid:
$query->add('price', 10)->as('withFlat');          // field + literal
$query->add('price', 'tax', 'fee')->as('total');   // field + fields
$query->add(5, 10, 15)->as('constantSum');         // literals only
For SUB and DIVIDE, the first operand is the initial value. Subsequent operands are subtracted from or divided into it in order.