Add round param for `wc_add_number_precision`

Let’s us return the cent value without rounding to an integer.
This commit is contained in:
Mike Jolley 2017-11-20 16:30:04 +00:00
parent 1bd30af0d0
commit 80ffcfb45d
1 changed files with 9 additions and 6 deletions

View File

@ -1495,11 +1495,13 @@ function wc_get_rounding_precision() {
* *
* @since 3.2.0 * @since 3.2.0
* @param float $value Number to add precision to. * @param float $value Number to add precision to.
* @return int * @param bool $round Should we round after adding precision?
* @return int|float
*/ */
function wc_add_number_precision( $value ) { function wc_add_number_precision( $value, $round = false ) {
$precision = pow( 10, wc_get_price_decimals() ); $precision = pow( 10, wc_get_price_decimals() );
return intval( round( $value * $precision ) ); $value = $value * $precision;
return $round ? intval( round( $value ) ) : $value;
} }
/** /**
@ -1519,15 +1521,16 @@ function wc_remove_number_precision( $value ) {
* *
* @since 3.2.0 * @since 3.2.0
* @param array $value Number to add precision to. * @param array $value Number to add precision to.
* @param bool $round Should we round after adding precision?
* @return int * @return int
*/ */
function wc_add_number_precision_deep( $value ) { function wc_add_number_precision_deep( $value, $round = false ) {
if ( is_array( $value ) ) { if ( is_array( $value ) ) {
foreach ( $value as $key => $subvalue ) { foreach ( $value as $key => $subvalue ) {
$value[ $key ] = wc_add_number_precision_deep( $subvalue ); $value[ $key ] = wc_add_number_precision_deep( $subvalue, $round );
} }
} else { } else {
$value = wc_add_number_precision( $value ); $value = wc_add_number_precision( $value, $round );
} }
return $value; return $value;
} }