diff --git a/percentify/stats.py b/percentify/stats.py index 158f951..8741c31 100644 --- a/percentify/stats.py +++ b/percentify/stats.py @@ -892,6 +892,9 @@ def split(total, weights, decimals: Optional[int] = 2): shares = w.astype(float) / weight_sum * float(total) if decimals is not None: shares = shares.round(decimals) + remainder = round(float(total), decimals) - float(shares.sum()) + if remainder: + shares.iloc[-1] = round(shares.iloc[-1] + remainder, decimals) return shares if is_series else shares.tolist() diff --git a/tests/test_stats.py b/tests/test_stats.py index 7ac2e01..2b81393 100644 --- a/tests/test_stats.py +++ b/tests/test_stats.py @@ -609,7 +609,9 @@ def test_split_list(): def test_split_equal(): - assert split(100, [1, 1, 1]) == [pytest.approx(33.33)] * 3 + result = split(100, [1, 1, 1]) + assert result == [33.33, 33.33, 33.34] + assert sum(result) == 100 def test_split_series_returns_series():