Number modifiers

Learn how to manipulate numeric values in CQL.

Number modifiers are natural language functions for manipulating numeric values. These functions are useful for tasks that involve transforming one thing into another, such as rounding a number, formatting it for display, and other similar tasks.

This is the summary of the available number modifiers for quick reference:

ModifierDescription
RoundRound a number to the nearest integer.

Round

This modifier rounds a number to the nearest whole number, based on the rounding mode and scale that you specify.

When you round a number, the output type remains the same as the input. For example, if you round a float value, the result will still be a float value. Rounding an integer has no effect, so the output is the same as the input.

Syntax

This modifier has the following syntax:

/*<value>*/ rounded
Try in Playground

You can optionally specify a rounding mode:

/*<value>*/ rounded /*<mode>*/
Try in Playground

And also a scale:

/*<value>*/ rounded with scale of /*<scale>*/
Try in Playground

The /*<mode>*/ is a literal word that specifies the rounding mode and cannot be specified dynamically using expressions.

Here is a list of the supported modes:

ModeDescription
upRound away from zero. For example, 2.5 becomes 3 and -2.5 becomes -3. This is the default rounding mode.
downRound towards zero. For example, 2.5 becomes 2 and -2.5 becomes -2.
half upRound to the nearest integer, with .5 rounded up. For example, 2.5 becomes 3, and -2.5 becomes -3.
half downRound to the nearest integer, with .5 rounded down. For example, 2.5 becomes 2, and -2.5 becomes -2.
half evenRound to the nearest integer, with .5 rounded to the nearest even number. For example, 2.5 becomes 2, and 3.5 becomes 4.
half oddRound to the nearest integer, with .5 rounded to nearest odd number. For example, 2.5 becomes 3, and 3.5 becomes 3.

Parameters

These are the supported parameters:

valuenumber

The number to round.

scale(optional)integer

A non-negative integer that specifies the number of digits to the right of the decimal point.

Examples

Here is a basic example of how to round a number using the default rounding mode:

10.5 rounded // 11
Try in Playground

For more control, you can specify the rounding mode:

10.5 rounded down // 10
Try in Playground

If you want to round a number to a specific number of decimal places, you can specify the scale:

10.136 rounded with scale of 2 // 10.14
Try in Playground