diff --git a/src/Format.php b/src/Format.php index 1e47e62..eff2b0d 100644 --- a/src/Format.php +++ b/src/Format.php @@ -270,8 +270,9 @@ public static function value( /** - * @param scalar|null $value - * @return scalar|null + * @template T of scalar|null + * @param T $value + * @return ($strict is true ? int|float|null : T) */ public static function numeric(mixed $value, ?int $precision = null, bool $strict = true): mixed { @@ -283,15 +284,15 @@ public static function numeric(mixed $value, ?int $precision = null, bool $stric $number = (float) $number; - if ((int) $number == $number) { - return (int) $number; + if ($precision !== null) { + $number = round($number, $precision); } - if (is_null($precision)) { - return $number; + if ($number == (int) $number) { // Intentionally used == for lax comparison + return (int) $number; } - return round($number, $precision); + return $number; }