From f167d17e9db32bf640d51723c0f1aec142a4c7a8 Mon Sep 17 00:00:00 2001 From: spaceox1000 Date: Tue, 18 Nov 2025 13:01:31 +0000 Subject: [PATCH 1/2] Update core.py --- percentify/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/percentify/core.py b/percentify/core.py index 4ae8dca..4a3d299 100644 --- a/percentify/core.py +++ b/percentify/core.py @@ -13,6 +13,6 @@ def percent(part: float, whole: float, decimals: int | None = 2) -> float: """ if whole == 0: - return 0.0 + raise ValueError("whole cannot be zero.") value = (part / whole) * 100 return round(value, decimals) if decimals is not None else value From d5d71aaa60b105214078406aef8f4a38a45ebcdd Mon Sep 17 00:00:00 2001 From: Daniel Emakporuena <97764732+Daniel15568@users.noreply.github.com> Date: Tue, 18 Nov 2025 13:15:43 +0000 Subject: [PATCH 2/2] Refactor percent function to accept SupportsFloat --- percentify/core.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/percentify/core.py b/percentify/core.py index 4a3d299..85375ae 100644 --- a/percentify/core.py +++ b/percentify/core.py @@ -1,18 +1,28 @@ -def percent(part: float, whole: float, decimals: int | None = 2) -> float: +from typing import SupportsFloat + +def percent(part: SupportsFloat, whole: SupportsFloat, decimals: int | None = 2) -> float: """ - Easily calculate what percentage `part` is of the `whole`. + Calculate what percentage `part` is of the `whole`. Args: - part (float): The numerator. - whole (float): The denominator. - decimals (int | None): Number of decimal places to round. - If None, no rounding is applied. + part: The numerator. + whole: The denominator. + decimals: Number of decimal places to round to. + If None, the raw percentage (unrounded float) is returned. Returns: - float: Percentage value. - + float: Percentage value. If `whole` is 0, returns 0.0. """ + whole = float(whole) if whole == 0: - raise ValueError("whole cannot be zero.") - value = (part / whole) * 100 - return round(value, decimals) if decimals is not None else value + return 0.0 + + value = float(part) / whole * 100.0 + + if decimals is None: + return value + + if decimals < 0: + raise ValueError("`decimals` must be non-negative or None.") + + return round(value, decimals)