Skip to content

Commit

Permalink
Improved handling of Format::numeric method
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk committed May 28, 2024
1 parent 3287cd1 commit 0f7ee65
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}


Expand Down

0 comments on commit 0f7ee65

Please sign in to comment.