-
-
Notifications
You must be signed in to change notification settings - Fork 18
Trim decimals #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Trim decimals #156
Conversation
samdark
commented
Jan 8, 2026
| Q | A |
|---|---|
| Is bugfix? | ❌ |
| New feature? | ✔️ |
| Breaks BC? | ❌ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a new NumericHelper::trimDecimalZeros() method that removes trailing zeros from the decimal part of numeric strings. The implementation handles various edge cases including null values, empty strings, negative numbers, and numbers without decimal parts.
- Adds
trimDecimalZeros()static method to NumericHelper class - Includes comprehensive test coverage with 10 test cases
- Updates documentation comment style for existing method
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/NumericHelper.php | Adds the new trimDecimalZeros() method with documentation, also fixes hyphenation in existing docblock |
| tests/NumericHelperTest.php | Adds test data provider and test method for the new functionality |
| CHANGELOG.md | Documents the new feature in the 2.7.1 release notes |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
Co-authored-by: samdark <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: samdark <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Sergei Tigrov <[email protected]>
diff --git c/src/NumericHelper.php i/src/NumericHelper.php index c602ede..2506fde 100644 --- c/src/NumericHelper.php +++ i/src/NumericHelper.php @@ -16,9 +16,6 @@ use function is_numeric; use function is_scalar; use function preg_match; use function preg_replace; -use function rtrim; -use function str_contains; -use function str_ends_with; use function str_replace; use function substr; @@ -176,7 +173,7 @@ final class NumericHelper * containing a decimal part. * * @return string|null The input string with trailing decimal zeros (and a trailing decimal - * separator, if any) removed, or `null` if the input was `null`. + * separator, if any) removed, or `null` if the input was `null` or an empty string. */ public static function trimDecimalZeros(?string $value): ?string {
diff --git c/src/NumericHelper.php i/src/NumericHelper.php index 2506fde..d1c388b 100644 --- c/src/NumericHelper.php +++ i/src/NumericHelper.php @@ -186,16 +186,26 @@ final class NumericHelper return null; } - if (!str_contains($value, '.')) { - return $value; - } - /** @psalm-suppress PossiblyNullArgument */ - $value = rtrim($value, '0'); - - if (!str_ends_with($value, '.')) { + $decimalPosition = strpos($value, '.'); + if ($decimalPosition === false) { return $value; } - return substr($value, 0, -1); + $length = strlen($value); + $index = $length - 1; + + while ($index > $decimalPosition && $value[$index] === '0') { + $index--; + } + + if ($index === $length - 1) { + return $value; + } + + if ($index === $decimalPosition) { + return substr($value, 0, $decimalPosition); + } + + return substr($value, 0, $index + 1); } }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
Co-authored-by: Sergei Predvoditelev <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
vjik
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add info to readme