wc_add_number_precision()
Add precision to a number by moving the decimal point to the right as many places as indicated by wc_get_price_decimals(). Optionally the result is rounded so that the total number of digits equals wc_get_rounding_precision() plus one.
No Hooks.
Returns
Int|float.
Usage
wc_add_number_precision( ?float $value, $round );
- ?float $value(required)
- .
- $round(true|false)
- If the result should be rounded.
Default:true
Changelog
| Since 3.2.0 | Introduced. |
wc_add_number_precision() wc add number precision code WC 10.6.2
function wc_add_number_precision( ?float $value, bool $round = true ) {
if ( ! $value ) {
return 0.0;
}
// Fallback to standard rounding precision in order to cover rounding changes in PHP 8.4.
$result = $value * pow( 10, wc_get_price_decimals() );
$round_precision = $round ? wc_get_rounding_precision() - wc_get_price_decimals() : wc_get_rounding_precision();
return NumberUtil::round( $result, $round_precision );
}