Rounding percents
Protobi rounds using the following method which covers a lot of cases where rounding errors occur due to the limited precision of binary arithmetic.
Protobi rounds to 6 decimals places first, then to your desired precision. In binary arithmetic 6/40 + 7/40 = 0.32499999999999996. This result rounds to 32% when shown as an integer percent. But if we round first to 0.325000, then to our desired precision the result is 33%.
fmtPercent (6/40 + 7/40, 0, true) ==> 33%
function fmtPercent(val, precision, percentSymbol) {
var result
if (val == null || isNaN(val)) {
result = '–'
}
else {
if (typeof precision == 'string') result = d3.format(precision)
else {
result = (+((val * 100).toFixed(6))).toFixed(precision || 0)
if (percentSymbol !== false) result += '%';
}
}
return result
}
Limited precision of arithmetic on computers
Rounding errors occur on computers due to the limited precision of binary mathematics. Most decimal fractions cannot be represented exactly as binary fractions.
For instance in binary arithmetic 6/40 + 7/40 = 0.32499999999999996 which is close to but not exactly equal to 0.325. The value the computer returns is just an approximation to the original decimal fraction.
This problem is easy to understand if you consider the fraction ⅓. You can approximate the decimal value to be 0.3, 0.33 or 0.333, etc. But no matter how many digits you write down you will never exactly represent ⅓.
Rounding raw numbers
Protobi rounds to the nearest integer
Rounding to the nearest integer is a simple mathematical operation we apply to decimal numbers. For decimal numbers between two integers like 1 and 2 , the calculation to round to the nearest integer is pretty straightforward. Decimals less than 0.5 are rounded down and decimals above 0.5 are rounded up.
You may know this rule from elementary school-- "5 and above, give it a shove. 4 and below, let it go". As long as the number immediately preceding the decimal separator is 4 or less, the number will round down to the nearest integer.
How Protobi rounds half decimals
For decimal numbers ending with exactly 0.5 there are two integers that are equally near. There are many different methods to handle rounding half numbers.
For these cases, Protobi uses a convention called round half away from zero, where decimals ending with 0.5 are always rounded to the integer with the highest absolute value. This method is also known as round half toward infinity or commercial rounding because it is often used for currency conversions and price rounding.
Rounding half away from zero treats positive and negative values symmetrically, and therefore is free of overall positive/negative bias.
Examples
Decimal number | Rounded integer |
---|---|
23.49 | 23 |
23.499 | 23 |
23.49999 | 23 |
23.499999999 | 23 |
23.5 | 24 |
-23.49 | -23 |
-23.499 | -23 |
-23.49999 | -23 |
-23.499999999 | -23 |
-23.5 | -24 |
Some clients have remarked that decimals like 23.49999 should first round to one decimal 23.5 then round to 24. This is known as double rounding and we do not use this method when displaying raw numbers from your survey datafile.
See the Wikipedia page for more information on Rounding half away from zero and other rounding methods.
We can replace the rounding algorithm in your projects with your firm's business rules. Contact support@protobi.com for more information.