Merge pull request #16391 from woocommerce/fix/php52-rounding-mode

Fake round half down in PHP 5,2
This commit is contained in:
Claudiu Lodromanean 2017-08-10 09:39:38 -07:00 committed by GitHub
commit 1745447c7c
2 changed files with 34 additions and 11 deletions

View File

@ -369,7 +369,19 @@ function wc_cart_round_discount( $value, $precision ) {
if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ) { if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ) {
return round( $value, $precision, WC_DISCOUNT_ROUNDING_MODE ); return round( $value, $precision, WC_DISCOUNT_ROUNDING_MODE );
} else { } else {
return round( $value, $precision ); // Fake it in PHP 5.2.
if ( 2 === WC_DISCOUNT_ROUNDING_MODE && strstr( $value, '.' ) ) {
$value = (string) $value;
$value = explode( '.', $value );
$value[1] = substr( $value[1], 0, $precision + 1 );
$value = implode( '.', $value );
if ( substr( $value, -1 ) === '5' ) {
$value = substr( $value, 0, -1 ) . '4';
}
$value = floatval( $value );
}
return round( $value, $precision );
} }
} }

View File

@ -216,21 +216,32 @@ function wc_trim_zeros( $price ) {
/** /**
* Round a tax amount. * Round a tax amount.
* *
* @param double $tax Amount to round. * @param double $value Amount to round.
* @param int $dp DP to round. Defaults to wc_get_price_decimals. * @param int $precision DP to round. Defaults to wc_get_price_decimals.
* @return double * @return double
*/ */
function wc_round_tax_total( $tax, $dp = null ) { function wc_round_tax_total( $value, $precision = null ) {
$dp = is_null( $dp ) ? wc_get_price_decimals() : absint( $dp ); $precision = is_null( $precision ) ? wc_get_price_decimals() : absint( $precision );
// @codeCoverageIgnoreStart if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ) {
if ( version_compare( phpversion(), '5.3', '<' ) ) { $rounded_tax = round( $value, $precision, WC_TAX_ROUNDING_MODE );
$rounded_tax = round( $tax, $dp );
} else { } else {
// @codeCoverageIgnoreEnd // Fake it in PHP 5.2.
$rounded_tax = round( $tax, $dp, WC_TAX_ROUNDING_MODE ); if ( 2 === WC_TAX_ROUNDING_MODE && strstr( $value, '.' ) ) {
$value = (string) $value;
$value = explode( '.', $value );
$value[1] = substr( $value[1], 0, $precision + 1 );
$value = implode( '.', $value );
if ( substr( $value, -1 ) === '5' ) {
$value = substr( $value, 0, -1 ) . '4';
}
$value = floatval( $value );
}
$rounded_tax = round( $value, $precision );
} }
return apply_filters( 'wc_round_tax_total', $rounded_tax, $tax, $dp, WC_TAX_ROUNDING_MODE );
return apply_filters( 'wc_round_tax_total', $rounded_tax, $value, $precision, WC_TAX_ROUNDING_MODE );
} }
/** /**