feat(cast): Add precision and rounding mode in FloatCast#10086
Draft
patel-vansh wants to merge 2 commits intocodeigniter4:4.8from
Draft
feat(cast): Add precision and rounding mode in FloatCast#10086patel-vansh wants to merge 2 commits intocodeigniter4:4.8from
patel-vansh wants to merge 2 commits intocodeigniter4:4.8from
Conversation
michalsn
reviewed
Apr 6, 2026
Member
michalsn
left a comment
There was a problem hiding this comment.
We currently need both casters. Entity/Cast handles Entity property casting, while DataCaster/Cast handles Model field casting. Even though Model casting has been the preferred approach since v4.5 and Entity casting may be deprecated in the future, Entity casting is still supported, so new float[...] behavior should be consistent in both places.
Also, the new language key is missing.
Comment on lines
+53
to
+57
| if (isset($modeMap[$modeParam])) { | ||
| $mode = $modeMap[$modeParam]; | ||
| } else { | ||
| throw CastException::forInvalidFloatRoundingMode($params[1]); | ||
| } |
Member
There was a problem hiding this comment.
Can we use match here and throw by default?
$mode = PHP_ROUND_HALF_UP; // Default mode
if (isset($params[1])) {
$mode = match (strtolower($params[1])) {
'up' => PHP_ROUND_HALF_UP,
'down' => PHP_ROUND_HALF_DOWN,
'even' => PHP_ROUND_HALF_EVEN,
'odd' => PHP_ROUND_HALF_ODD,
default => throw CastException::forInvalidFloatRoundingMode($params[1]),
};
}
Contributor
|
Yes, a useful change. Don't forget to update the user guide examples |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adds ability to set precision as well as rounding mode during Float casting.
So, the float cast can be use as following:
I haven't updated the user guide yet (that's why the PR is in draft), as before updating the user guide, I wanted others to give their opinion on the implementation.
Also, I had one question, I saw that the casts are defined at two places in
system/folder:I don't know why there are two different implementation of similar classes, that's why for now, I haven't added the rounding code in
Entity/Cast/FloatCast.php, but if its needed, then I'll add (however, Idk if the CPD Github Action will catch it and fail)Checklist: