> ## Documentation Index
> Fetch the complete documentation index at: https://gobl-improv.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Rounding

Rounding in business documents can be surprisingly complex. Although it seems simple, different rules and interpretations can lead to varying results across companies and applications.

GOBL aims to minimize rounding errors by focusing on three key areas:

* General number rounding.
* Generating totals.
* Calculating taxes.

We'll explore each area below, but first, let's discuss precision.

## Precision

Effective rounding requires knowing the level of precision. GOBL uses the [num package](https://github.com/invopop/gobl/tree/main/num), as detailed in the [numbers section](/overview/numbers), to ensure every number has a fixed number of decimal places.

For example, `1.10` has 2 decimal places, while `1.1020` has 4. Precision significantly impacts calculations. Consider removing 21% from a price of 10 with 2 decimal places, then re-adding it:

* `10.00` ÷ `1.21` = `8.26`
* `8.26` × `1.21` = `9.99`

We've lost 1 cent! Now with 4 decimal places:

* `10.0000` ÷ `1.21` = `8.2645`
* `8.2645` × `1.21` = `10.0000`

This example shows how precision can prevent compounding errors.

## General Rounding

Rounding reduces a number's precision by trimming excess digits. A simple method is truncation, where `8.2556` becomes `8.25`, but this can cause calculation errors. A better approach is rounding to the nearest number. If the excess digits are 0.50 or higher, round up; otherwise, round down. For example:

* `8.2551` becomes `8.26`.
* `8.2550` becomes `8.26`.
* `8.2549` becomes `8.25`.

While other [rounding techniques](https://en.wikipedia.org/wiki/Rounding) exist, GOBL uses this common method for consistency.

## Totals

When a document like an invoice contains a set of line amounts and references to other details such as payments and advances, by default GOBL ensures that all calculations are made using the currency's base precision plus 2 decimal places. If the lines in the document are defined with even greater precision, this will be maintained in all further calculations.

For example, if you're dealing with Euros or US Dollars, the final precision for calculations would be 4. The Japanese Yen (YEN) or Chile's Peso (CLP) whose currencies do not use cents, would only have 2 decimal places added.

Totals in GOBL will **always** be presented using the currency's default precision. This is important as most tax agencies do not recognize a level of precision that cannot be dealt with using the local coinage.

GOBL by default uses a rounding method called `sum-then-round` when calculating a document's final totals. This implies that all calculations are made with the maximum amount of precision, before rounding in the output.

An unexpected consequence of this however is that sometimes the presented numbers might not add up and be a few cents out. For example, take the following invoice lines:

```json
[
  {
    "i": 1,
    "quantity": "20.10",
    "item": {
      "name": "Service 1",
      "price": "3.05"
    },
    "sum": "61.31",
    "total": "61.31"
  },
  {
    "i": 2,
    "quantity": "20.10",
    "item": {
      "name": "Service 2",
      "price": "3.05"
    },
    "sum": "61.31",
    "total": "61.31"
  }
]
```

When that data is plugged into a GOBL invoice the final sum of the invoice is `122.61` not `122.62` as would be expected by summing the line totals. We can see why if we add a `0` to each item's price:

```json
[
  {
    "i": 1,
    "quantity": "20.10",
    "item": {
      "name": "Service 1",
      "price": "3.050"
    },
    "sum": "61.305",
    "taxes": [
      {
        "cat": "VAT",
        "percent": "23.0%"
      }
    ],
    "total": "61.305"
  },
  {
    "i": 2,
    "quantity": "20.10",
    "item": {
      "name": "Service 2",
      "price": "3.050"
    },
    "sum": "61.305",
    "taxes": [
      {
        "cat": "VAT",
        "percent": "23.0%"
      }
    ],
    "total": "61.305"
  }
]
```

GOBL supports a second rounding method called `round-then-sum` which as the name suggests, will first round the numbers to the expected precision before making the totals calculations. This ensures that the presented totals can always be recalculated independently, but may result in unexpected final amounts if the original prices were presented including tax.

When converting GOBL into to other formats, you may need to override the default to align with expectations from clients. Some tax regimes like Greece (`EL`) require th rounding method and it will be chosen automatically. While we don't recommend this, it is possible in some document types to override the default. To apply the `round-then-sum` rounding method, update the document's tax object with:

```json
{
  "tax": {
    "rounding": "round-then-sum"
  }
}
```

## Calculating Taxes

When adding sales tax or VAT to an invoice, you need to decide where the taxes should be applied. There are three main options:

* **Per Item**: Calculate the tax for each item before considering quantity or discounts.
* **Per Line**: Calculate the tax for each line's total, a method commonly endorsed by many countries.
* **On Total**: Group all line totals by tax category and rate, then calculate the tax. This method, recommended by EN 16931, provides the greatest precision.

GOBL **only calculates tax amounts on totals** to minimize rounding errors. In practice, as long as calculations use sufficient precision, there is little difference between the line and total methods.

As with other total calculations, taxes are calculated with a precision of at least the currency's base number of decimal places plus 2.
