> ## Documentation Index
> Fetch the complete documentation index at: https://docs.effectiveai.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Round a premium

> Round a value the way the filing says, choose the rounding mode, cite the rounding rule, and round after each factor in a chain.

[Install Relativities and download the synthetic examples](/relativities/setup) before running this page.

This page rounds values with `round(x, places, mode)`. RSL keeps every value exact until a
`round` changes it. So the premium rounds exactly where the filing says, and only there.

## Round to cents or dollars

`round` takes three parts: the value, the number of decimal places, and the mode.

```rsl theme={null}
            premium: Premium [cite: manual:p9] = round(self.coverage_a / 1000 * 3.125, 0, half_up)
```

* `2` rounds to cents, `0` to whole dollars, and `-1` to tens of dollars.
* The mode is required. Write the mode the filing states.
* A value already on the target, such as 2.10 rounded to cents, stays the same in every mode.

## Choose the mode

| Mode | Rounds |
| - | - |
| `half_up` | to the nearest; a half goes away from zero |
| `half_down` | to the nearest; a half goes toward zero |
| `half_even` | to the nearest; a half goes to the even digit |
| `up` | away from zero |
| `down` | toward zero |
| `ceiling` | up, toward the larger number |
| `floor` | down, toward the smaller number |

This rater rounds one amount to cents in each mode:

```rsl theme={null}
        step rounding [cite: manual:p9] {
            half_up: Premium [cite: manual:p9] = round(self.amount, 2, half_up),
            half_even: Num [cite: manual:p9] = round(self.amount, 2, half_even),
            half_down: Num [cite: manual:p9] = round(self.amount, 2, half_down),
            up: Num [cite: manual:p9] = round(self.amount, 2, up),
            down: Num [cite: manual:p9] = round(self.amount, 2, down),
            ceiling: Num [cite: manual:p9] = round(self.amount, 2, ceiling),
            floor: Num [cite: manual:p9] = round(self.amount, 2, floor)
        }
```

Rate 2.125, -2.125 and 2.121. The sample download includes this rater in
`relativities-examples/rsl/round-modes`:

```python theme={null}
import json

import rslc

rater = rslc.PreparedRater("relativities-examples/rsl/round-modes")
for amount in [2.125, -2.125, 2.121]:
    print(amount, rater.rate(json.dumps({"Policy": {"amount": amount}})))
# 2.125 {"ok":true,"outputs":{"Policy":{"half_up":2.13,"half_even":2.12,"half_down":2.12,"up":2.13,"down":2.12,"ceiling":2.13,"floor":2.12}}}
# -2.125 {"ok":true,"outputs":{"Policy":{"half_up":-2.13,"half_even":-2.12,"half_down":-2.12,"up":-2.13,"down":-2.12,"ceiling":-2.12,"floor":-2.13}}}
# 2.121 {"ok":true,"outputs":{"Policy":{"half_up":2.12,"half_even":2.12,"half_down":2.12,"up":2.13,"down":2.12,"ceiling":2.13,"floor":2.12}}}
```

The modes differ on a half, such as 2.125, and on negative amounts.

## Round the premium and cite the rule

Each premium the rater returns passes through a `round`. A premium with a step after its last
`round` fails the check:

```text theme={null}
FAIL rater.rsl (strict)
error: E-OUTCOME-ROUND rater.rsl:22:13: rating outcome `premium` (Policy.premium) does not pass through an explicit round. State the filing's premium rounding rule with round(x, SCALE, MODE) and cite it, for example `round(x, 0, half_up)` for the nearest whole dollar or `round(x, 2, half_up)` for the nearest cent
```

The calculation that rounds the premium also cites the rounding rule, on the calculation itself.
A citation on the whole step falls short:

```text theme={null}
FAIL rater.rsl (strict)
error: E-OUTCOME-ROUND rater.rsl:22:13: the round that produces rating outcome `premium` sits in `Policy.premium`, which has no attribute citation. A step citation does not name the rounding rule. Cite the filed rounding rule on the attribute itself: `premium: T [cite: source:region] = round(…)`
```

Write the citation after the type, before `=`, as the first example on this page does.

## Round after each factor

Some filings multiply a chain of factors and round after each one. Write a `product` with `by`,
to fix the order, and `round_each`:

```rsl theme={null}
            combined: Factor [cite: manual:p10] =
                product(self.factors as f by f.order, f.value, round_each: (4, half_up)),
```

The rater sorts the factors by `order`, multiplies them in turn, and rounds to 4 places after each
step. Two factors of 1.00005 give 1.0002. One round at the end gives 1.0001. The sample download includes
this rater in `relativities-examples/rsl/round-stepwise`:

```python theme={null}
rater = rslc.PreparedRater("relativities-examples/rsl/round-stepwise")
policy = {"Policy": {"base": 500, "factors": [{"order": 1, "value": 1.00005}, {"order": 2, "value": 1.00005}]}}
print(rater.rate(json.dumps(policy), detailed=True))
# {"ok":true,"outputs":{"Policy":{"factors":[{},{}],"combined":1.0002,"premium":500.1}}}
```

* `by` names a number or date input of each element.
* Each `order` value appears once. The rater refuses a repeat with `INPUT-KEY` on a `keyed by` list, and with `INPUT-UNIQUE` on other lists.
* `round_each` works with `sum` and `product`.

## Related

* [Exact numbers](/relativities/rsl/explanation/exact-numbers) explains why values stay exact until a `round`.
* [Citations](/relativities/rsl/explanation/citations) lists each value that needs a citation.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.