round
round, function
def const dash autodiff pure round(quantity: number): number
def const dash autodiff pure round(quantity: number, digits: number): number
Rounds quantity to the specified number of decimal digits (default is 0).
Ties are rounded to the nearest even value.
quantity: the value to round.digits: the number of digits after the decimal point.
Examples
show summary "round" with
round(0.5) as "0.5"
round(0.51) as "0.51"
round(1.5) as "1.5"
round(4.5) as "4.5"
round(-0.5) as "-0.5"
round(-0.51) as "-0.51"
round(-1.5) as "-1.5"
This outputs the following summary:
| 0.5 | 0.51 | 1.5 | 4.5 | -0.5 | -0.51 | -1.5 |
|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 4 | 0 | -1 | -2 |
show summary "round digits" with
round(4.33, 0) as "0 digits"
round(4.33, 1) as "1 digit"
round(4.33, 2) as "2 digits"
This outputs the following summary:
| 0 digits | 1 digit | 2 digits |
|---|---|---|
| 4 | 4.3 | 4.33 |
Remarks
Calling round(x) or round(x, 0) gives identical results.
The digits argument must be an integer value; fractional decimals are not allowed.
The rounding method is commonly known as bankers rounding. Floating-point
representation can affect borderline cases. For example, 0.05 is stored as
0.0500000007 in float32, so round(0.05, 1) yields 0.1.
When used inside an autodiff block, the gradient
associated to round() is 1 to facilitate discrete policies.
Recipes and best practices
To round numbers displayed in tiles, prefer StyleCode precision or minPrecision.