Skip to content

feat(cast): Add precision and rounding mode in FloatCast#10086

Open
patel-vansh wants to merge 3 commits intocodeigniter4:4.8from
patel-vansh:feat/float-cast-precision
Open

feat(cast): Add precision and rounding mode in FloatCast#10086
patel-vansh wants to merge 3 commits intocodeigniter4:4.8from
patel-vansh:feat/float-cast-precision

Conversation

@patel-vansh
Copy link
Copy Markdown
Contributor

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:

$casts = [
    // ...
    'price' => 'float[2]',
    'price_rounded_even' => 'float[2,even]',
    // ...
];

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:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@github-actions github-actions bot added the 4.8 PRs that target the `4.8` branch. label Apr 6, 2026
Copy link
Copy Markdown
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@neznaika0
Copy link
Copy Markdown
Contributor

Yes, a useful change. Don't forget to update the user guide examples

@patel-vansh
Copy link
Copy Markdown
Contributor Author

patel-vansh commented Apr 8, 2026

I've currently copied the same code for Entity/Cast/FloatCast, if it gets caught by CPD, I think we can call the DataCaster function directly, or maybe tweak a little bit to make it somewhat different. For documentation, I've added a line in Scalar Type Casting section, but I'm unsure if its okay to introduce another section with just float cast showing its parameters or not. @michalsn and @neznaika0

@patel-vansh patel-vansh marked this pull request as ready for review April 8, 2026 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants