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 (2) 

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 (2) 

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]=

RoundedPercentageShare can be applied to an Association of numerical values:

In[7]:=
ResourceFunction["RoundedPercentageShare"][<|"a" -> 1, "b" -> 2|>]
Out[7]=

Publisher

Jon McLoone

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.1.0 – 23 October 2024
  • 1.0.0 – 11 February 2019

License Information