round-percentages

How To Round Percentages

No matter if you just started learning how to round numbers now or if you have already learned this for quite some time, you may be wondering about how you can round percentages.

Learn how to round numbers up and down.

round-percentages

While most people don’t need to round percentages, some do. Imagine that you’re doing a school project or a project for work and you need to deal with percentages. So, at some point, your teacher or employer tells you that you need to use percentages when you are displaying the scores. As you probably already know, the percentage should be relative to the total score. 

At first sight, this doesn’t seem difficult at all. So, let’s check a simple example. 

Imagine that you have the following percentages:

18.562874251497007%

20.958083832335326%

18.562874251497007%

19.161676646706585%

22.75449101796407%

To ensure that you can represent them in a graph, it’s better to round them off to show whole numbers. So, according to the rules you already know:

19%

21%

19%

19%

23%

So everything seems perfect. But hold up one sec. That doesn’t add up to 100%! It actually adds up to 101%! 

Understanding unexpected results when rounding numbers.

This is a big problem with blind rounding. 

So, how can you guarantee that the percentage results will always add up to 100%?

How To Round Percentages

The truth is that you have plenty of different options to round percentages. However, instead of showing you long, tedious, and even difficult methods, we decided to take a simpler approach. 

It’s important to keep in mind that we’re doing this because, in this particular example, the reliance on the original decimal point isn’t critical. 

Learn how to round fractions.

So, the best thing you can do in this case is to use the Largest Remainder Method algorithm. This is a very simple algorithm that consists of:

Largest-Remainder-Method
  1. Rounding all values down to the nearest integer value;
  2. Determining the difference between the sum of the rounded values and total value;
  3. Distributing the difference between the rounded values in decreasing order of their decimal parts.

Considering the initial dataset:

dataset = [18.562874251497007, 20.958083832335326, 18.562874251497007, 19.161676646706585, 22.75449101796407]

Let’s start by rounding down all values:

=> [18, 20, 18, 19, 22]

Now the difference between the sum of the rounded values and total value (in this case 100%):

diff = 100 – rounded_values.reduce(&:+) => 3

This means that you have to distribute 3% across your rounded percentages. 

The algorithm says that this distribution should be done in decreasing order of the values’ decimal part, so let’s first sort them by their decimal part:

sorted_dataset = dataset.sort_by { |x| x.floor – x }

=> [20.958083832335326, 22.75449101796407, 18.562874251497007, 18.562874251497007, 19.161676646706585]

And finally distribute the remaining 3%:

final_percentages = sorted_dataset.map.with_index do |e, index|

  index < diff ? e.floor + 1 : e.floor

end

The result is:

=> [21, 23, 19, 18, 19]

which adds up to 100(%)!

Discover how to round numbers in C and C++.

how-to-round-percentages

This is a relatively easy and fast way to ensure that rounded percentages add up to 100%. Again, this is not the best rounding algorithm considering that we pretty much disregard the decimal part but I would say that for most cases this is good enough. Finally I’ll leave here a more condensed version of the algorithm:

dataset = [18.562874251497007, 20.958083832335326, 18.562874251497007, 19.161676646706585, 22.75449101796407]

diff = 100 – dataset.map(&:floor).reduce(&:+)

rounded_percentages = dataset

  .sort_by { |x| x.floor – x}

  .map

  .with_index { |e, index| index < diff ? e.floor + 1 : e.floor }

© 2021 rounding.to . Powered by WordPress. Theme by Viva Themes.