- General number rounding.
- Generating totals.
- Calculating taxes.
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)
10.0000
÷1.21
=8.2645
8.2645
×1.21
=10.0000
(no loss)
General Rounding
Rounding reduces a number’s precision by trimming excess digits. A simple method is truncation, where8.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
becomes8.26
.8.2550
becomes8.26
.8.2549
becomes8.25
.
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: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:
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:
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.