Wolfram Research

Function Repository Resource:

RoundedPercentageShare

Source Notebook

Calculate rounded percentages that always sum to 100%

Contributed by: Jon McLoone

ResourceFunction["RoundedPercentageShare"][data]

generates a list corresponding to the percentage share of the total that each item contributes, ensuring that the result always adds up to exactly 1.

Examples

Basic Examples

If you simply calculate percentages and round them, values do not always add up to 1. In this case the total is 0.99:

In[1]:=
data = {1, 1, 1};
Round[data/Total[data], 0.01]
Out[2]=
In[3]:=
Total[%]
Out[3]=

RoundedPercentageShare always sums to 1 by allocating spare percentages to the largest contributor:

In[4]:=
ResourceFunction["RoundedPercentageShare"][{1, 1, 1}]
Out[4]=

Scope

A second argument controls the amount of rounding:

In[5]:=
ResourceFunction["RoundedPercentageShare"][{1, 1, 1}, 0.1]
Out[5]=
In[6]:=
ResourceFunction["RoundedPercentageShare"][{1, 1, 1}, 0.001]
Out[6]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information