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, as detailed in the numbers section, to ensure every number has a fixed number of decimal places.

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

  • 10.00 ÷ 1.21 = 8.26
  • 8.26 × 1.21 = 9.99 (one cent loss)

We’ve lost one cent! Now with four decimal places:

  • 10.0000 ÷ 1.21 = 8.2645
  • 8.2645 × 1.21 = 10.0000 (no loss)

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 exist, GOBL uses this common method for consistency.

Total Calculations

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. The way the calculations are made is important.

When a document like an invoice contains a set of line amounts and references to other details such as payments and advances, GOBL offers two specific rounding techniques for making calculations:

  • precise - the default, with two additional decimal places, and,
  • currency - use the precision of the currency.

Precise Model

When using the “precise” model and the lines in the document are defined with even greater precision, it 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 four. The Japanese Yen (YEN) or Chile’s Peso (CLP) whose currencies do not use cents, would only have two decimal places added.

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

[
  {
    "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:

[
  {
    "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"
  }
]

Currency Rounding

GOBL optionally supports “currency” rounding, which will first round the numbers to the currency’s 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.

This rounding method is required by the European Norm 16931-1:2017 standard.

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 the rounding method and it will be chosen automatically. To apply the currency rounding method, update the document’s tax object with:

{
  "tax": {
    "rounding": "currency"
  }
}

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 will be calculated using the rounding method selected for the document.