NumberUtil::round( mixed $val, int $precision, int $mode = PHP_ROUND_HALF_UP )

Round a number using the built-in round function, but unless the value to round is numeric (a number or a string that can be parsed as a number), apply ‘floatval’ first to it (so it will convert it to 0 in most cases).

Description Description

This is needed because in PHP 7 applying round to a non-numeric value returns 0, but in PHP 8 it throws an error. Specifically, in WooCommerce we have a few places where round(”) is often executed.

Parameters Parameters


(Required) The value to round.


(Required) The optional number of decimal digits to round to.


(Optional) A constant to specify the mode in which rounding occurs.

Default value: PHP_ROUND_HALF_UP

Top ↑

Return Return

(float) The value rounded to the given precision as a float, or the supplied default value.

Top ↑

Source Source

File: src/Utilities/NumberUtil.php

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.