I participated in a Business to Business (B2B) project, where I was part of the team that developed the backoffice platform, and another team developed Frontoffice (the e-commerce store). Each part of the platform - Back and Front Office needed to exchange data and it was necessary to establish a numbering rounding rule, defining decimal places, for the calculations of orders with freight percentages, platform comission and taxes.
Every software that performs numerical calculations in the financial context needs to determine rounding of decimal places.This is necessary by market convention, or by law.
Therefore it is necessary to define rules to be followed so that the results can be foreseen and audited.
Another example of the need to establish rules for numbering rounding, in the context of computer systems occurs when there is communication between systems, with data exchange.
To meet all these contexts, the Brazilian Association of Technical Standards - ABNT, for rounding decimal numbering, can be applied effectively.
When to numeric round the decimal places
In statistics related calculations, financial mathematics among other situations where software solves everyday problems, business or even in the context of education and that are related to the use of numbers with decimal place limits.
Rules for rounding in decimal numbering
To define standard in the numerical rounding of decimal places, the Brazilian Association of Technical Standards (ABNT), establishes the NBR 5891:2014 to this. However, there is a standard universal rule used by many programming languages.
Simple rule for rounding decimal places
When the digit to be conserved is followed by a digit less than 5
, the digit remains conserved and the posterior ones removed. When it is larger than 5
, it adds a 1
to the digit you want to round.
Example: To round 3 decimal places in 2 in the number
7,421
, as the third digit is1
, less than5
, keep the second digit, the number2
, and remove the third one, resulting in7.42
.Example: To round 3 decimal places in 2 in the number
7.425
, as the third digit is5
, superior or equal to5
, add1
to the second digit, result7.43
.
ABNT NBR 5891/2014
The normative establishes the following points for this rule:
- When the digit to be conserved is followed by a digit less than
5
, the digit remains conserved and the posterior ones removed.- Example: To round 3 decimal places in 2 in the number
7,421
, as the third digit is1
, less than5
, the second digit, the number2
is kept and the third, is eliminated, resulting in7.42
.
- Example: To round 3 decimal places in 2 in the number
- When the digit to be conserved is followed by a digit greater than
5
, or equal to5
followed by at least one digit different from zero, a unit is added to the digit to be conserved and the posterior ones are removed.- Example: Round to two decimal places the number
23,52621
. The third number is tested, the number6
, as is greater than or equal to5
, adds1
to the second digit which is the number2
, resulting in23.53
. - Example: Round to two decimal places the number
5,8451
, as the third digit is5
, followed by a different number of zero0
, the second house is rounding more1
, resultingIn:5.85
.
- Example: Round to two decimal places the number
- When the digit to be kept is odd, followed by
5
and later of zeros, adds a unit to the digit to be conserved and the posterior ones are removed.- Example: Round to four decimal places the number
4,3241500
, the result is:4,3242
.
- Example: Round to four decimal places the number
- When the digit is conserved to be pair, followed by
5
subsequently of zeros, the digit remains to be conserved and the later removed.- Example: Round to one decimal digit the number
4,850
, the result is:4.8
.
- Example: Round to one decimal digit the number
Scripts and functions to ABNT NBR 5891/2014
If you need to follow ABNT NBR 891/2014 numerical rounding rules, the opens source community has implemented it on some languages as follow:
PHP script to ABNT NBR 5891/2014
We can found two PHP scripts on Github that implement functions to roung following ABNT NBR 5891/2014 rules:
VB6 - Visual Basic script to ABNT NBR 5891/2014
We can found one VB6 script on Github that implement functions to roung following ABNT NBR 5891/2014 rules:
Conclusions
Clearly define the rule to follow in cases of rounding in your software, according to the standardization required by the business rule it supports.
Comments