# CakePHP 5 - NumberHelper::currency() throwing error when value is null

**URL:** <https://discourse.cakephp.org/t/cakephp-5-numberhelper-currency-throwing-error-when-value-is-null/12107>\
**Category:** Uncategorized\
**Created:** [July 9, 2024, 10:09am UTC](https://discourse.cakephp.org/t/cakephp-5-numberhelper-currency-throwing-error-when-value-is-null/12107 "2024-07-09T10:09:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kez](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kez/32/3156_2.png) [@Kez](https://discourse.cakephp.org/u/Kez)\
**Post date:** [July 9, 2024, 10:09am UTC](https://discourse.cakephp.org/t/cakephp-5-numberhelper-currency-throwing-error-when-value-is-null/12107/1 "2024-07-09T10:09:26Z")

</div>

Hi all,

I have been working through migrating my app to CakePHP 5. One major breaking change I’ve run into is, whenever I call `$this->Number->currency($value)` where $value is null I get an error. Previously this method would treat null as a zero value and give me e.g. $0.00

I’m not sure if this is an explicit change or just a side-effect of upgrading to PHP 8.2 or using more strict typing or something. Presumably this is the expected behaviour? My workaround seems to be replacing every instance like this with `$value ?? 0` or something to that effect. I’m not sure if I could possibly override the number helper and allow it to convert null to 0 before calling the parent?

Thanks

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [July 9, 2024, 1:28pm UTC](https://discourse.cakephp.org/t/cakephp-5-numberhelper-currency-throwing-error-when-value-is-null/12107/2 "2024-07-09T13:28:50Z")

</div>

Thats expected. You need to check the value before outputting it via the number helper.

---

<div class="post-metadata">

**Author:** ![Rods](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@Rods](https://discourse.cakephp.org/u/Rods)\
**Post date:** [July 11, 2024, 1:28am UTC](https://discourse.cakephp.org/t/cakephp-5-numberhelper-currency-throwing-error-when-value-is-null/12107/3 "2024-07-11T01:28:15Z")

</div>

try this

$this-\>Number-\>currency(@$value) or $this-\>Number-\>currency(doubleval(@$value))

---

<div class="post-metadata">

**Author:** ![Salines](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/salines/32/3420_2.png) [@Salines](https://discourse.cakephp.org/u/Salines)\
**Post date:** [July 11, 2024, 11:15am UTC](https://discourse.cakephp.org/t/cakephp-5-numberhelper-currency-throwing-error-when-value-is-null/12107/4 "2024-07-11T11:15:27Z")

</div>

> [@Kez](#):
>
> $value

Why don’t you simply use mutators in your entity where you can define how the value will be displayed through the getter method? For example:

```auto
    // Getter for the 'value' field
    protected function _getValue($value)
    {
        // Format the value as you want it to be displayed
        return $value ?? 0;
    }

    // Setter for the 'value' field
    protected function _setValue($value)
    {
        // Return the original value without formatting for saving to the database
        return $value;
    }

```

---

<div class="post-metadata">

**Author:** ![Kez](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kez/32/3156_2.png) [@Kez](https://discourse.cakephp.org/u/Kez)\
**Post date:** [July 11, 2024, 1:58pm UTC](https://discourse.cakephp.org/t/cakephp-5-numberhelper-currency-throwing-error-when-value-is-null/12107/5 "2024-07-11T13:58:37Z")

</div>

Oh yeah that makes sense! Thanks. It would definitely tidy up my templates to do it this way!

Fortunately this only really comes up when I am joining aggregated data onto my queries, so I won’t need to worry about setters.

---

<div class="post-metadata">

**Author:** ![dereuromark](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dereuromark/32/37_2.png) [@dereuromark](https://discourse.cakephp.org/u/dereuromark)\
**Post date:** [July 13, 2024, 9:18am UTC](https://discourse.cakephp.org/t/cakephp-5-numberhelper-currency-throwing-error-when-value-is-null/12107/6 "2024-07-13T09:18:01Z")

</div>

Dont use @ - thats a dangerous code smell, as it will usually throw exceptions on type issues.  
Instead use ternary:

```auto
<?= $order->total ? $this->Number->currency($order->total) : '-' ?>

```

for non-int values (e.g. decimal or float)

You can check otherwise for !== null here.
