# Why does DecimalType::toPHP() return a string?

**URL:** <https://discourse.cakephp.org/t/why-does-decimaltype-tophp-return-a-string/8910>\
**Category:** Uncategorized\
**Created:** [January 24, 2021, 6:19am UTC](https://discourse.cakephp.org/t/why-does-decimaltype-tophp-return-a-string/8910 "2021-01-24T06:19:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![etipaced](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/etipaced/32/3338_2.png) [@etipaced](https://discourse.cakephp.org/u/etipaced)\
**Post date:** [January 24, 2021, 6:19am UTC](https://discourse.cakephp.org/t/why-does-decimaltype-tophp-return-a-string/8910/1 "2021-01-24T06:19:24Z")

</div>

I’m assuming it has to do with the potential to lose precision when converting from a database decimal into a PHP float so the solution was to cast it into a string instead?

[https://api.cakephp.org/4.0/class-Cake.Database.Type.DecimalType.html#toPHP](https://api.cakephp.org/4.0/class-Cake.Database.Type.DecimalType.html#toPHP)

If that’s the reason, I wonder if creating accessors for these in the entity is a better way to handle type conversion instead of having to add `(float)` when I need explicit type checks.

---

<div class="post-metadata">

**Author:** ![ADmad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/admad/32/20_2.png) [@ADmad](https://discourse.cakephp.org/u/ADmad)\
**Post date:** [January 25, 2021, 2:24pm UTC](https://discourse.cakephp.org/t/why-does-decimaltype-tophp-return-a-string/8910/2 "2021-01-25T14:24:28Z")

</div>

> I’m assuming it has to do with the potential to lose precision when converting from a database decimal into a PHP float so the solution was to cast it into a string instead?

Yes

> … instead of having to add `(float)`

Casting to float defeats the purpose of using decimal type.

---

<div class="post-metadata">

**Author:** ![etipaced](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/etipaced/32/3338_2.png) [@etipaced](https://discourse.cakephp.org/u/etipaced)\
**Post date:** [January 25, 2021, 4:57pm UTC](https://discourse.cakephp.org/t/why-does-decimaltype-tophp-return-a-string/8910/3 "2021-01-25T16:57:21Z")

</div>

> Casting to float defeats the purpose of using decimal type.

How so exactly?

I’m aware that floating point values can be non-exact when performing calculations on them, but this is a direct assignment. How is precision lost in that case?

Furthermore, how should one perform calculations on that value when it’s a string? It must be cast into a float for that.

---

<div class="post-metadata">

**Author:** ![ADmad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/admad/32/20_2.png) [@ADmad](https://discourse.cakephp.org/u/ADmad)\
**Post date:** [January 25, 2021, 6:26pm UTC](https://discourse.cakephp.org/t/why-does-decimaltype-tophp-return-a-string/8910/4 "2021-01-25T18:26:41Z")

</div>

> I’m aware that floating point values can be non-exact when performing calculations on them, but this is a direct assignment. How is precision lost in that case?

Just assignment in most cases won’t cause any problem as long as the value in within the range of `PHP_FLOAT_MIN` and `PHP_FLOAT_MAX`.

> Furthermore, how should one perform calculations on that value when it’s a string? It must be cast into a float for that.

No, use [BC Math](https://www.php.net/manual/en/ref.bc.php) functions or dedicated extensions like [PHP Decimal](https://php-decimal.io). There’s also [GitHub - moneyphp/money: PHP implementation of Fowler's Money pattern.](https://github.com/moneyphp/money).

---

<div class="post-metadata">

**Author:** ![etipaced](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/etipaced/32/3338_2.png) [@etipaced](https://discourse.cakephp.org/u/etipaced)\
**Post date:** [January 25, 2021, 10:26pm UTC](https://discourse.cakephp.org/t/why-does-decimaltype-tophp-return-a-string/8910/5 "2021-01-25T22:26:41Z")

</div>

> No, use [BC Math](https://www.php.net/manual/en/ref.bc.php) functions or dedicated extensions

Not sure why I didn’t think of that as I’ve used some in the past. At least the reason for casting into a string makes more sense now given the potential for precision loss in case of range overages.

Thank you for your assistance.
