$decPos) { return '0'; } $digitsBeforeDot = $length - ($decPos + 1); if ($precision >= ($length - ($decPos + 1))) { return $op1; } if ($precision === 0) { $triggerPos = 1; $roundPos = -1; } elseif ($precision > 0) { $triggerPos = $precision + 1; $roundPos = $precision; } else { $triggerPos = $precision; $roundPos = $precision -1; } $triggerDigit = $op1[$triggerPos + $decPos]; if ($precision < 0) { // zero fill digits to the left of the decimal place $op1 = substr($op1, 0, $decPos + $precision) . str_pad('', abs($precision), '0'); } if ($triggerDigit >= '5') { if ($roundPos + $decPos == -1) { return str_pad('1', $decPos + 1, '0'); } $roundUp = str_pad('', $length, '0'); $roundUp[$decPos] = '.'; $roundUp[$roundPos + $decPos] = '1'; return bcadd($op1, $roundUp, $precision); } elseif ($precision >= 0) { return substr($op1, 0, $decPos + ($precision ? $precision + 1: 0)); } return (string) $op1; } /** * Normalizes an input to standard english notation * Fixes a problem of BCMath with setLocale which is PHP related * * @param integer $value Value to normalize * @return string Normalized string without BCMath problems */ public static function normalize($value) { $convert = localeconv(); $value = str_replace($convert['thousands_sep'], "",(string) $value); $value = str_replace($convert['positive_sign'], "", $value); $value = str_replace($convert['decimal_point'], ".",$value); if (!empty($convert['negative_sign']) and (strpos($value, $convert['negative_sign']))) { $value = str_replace($convert['negative_sign'], "", $value); $value = "-" . $value; } return $value; } /** * Localizes an input from standard english notation * Fixes a problem of BCMath with setLocale which is PHP related * * @param integer $value Value to normalize * @return string Normalized string without BCMath problems */ public static function localize($value) { $convert = localeconv(); $value = str_replace(".", $convert['decimal_point'], (string) $value); if (!empty($convert['negative_sign']) and (strpos($value, "-"))) { $value = str_replace("-", $convert['negative_sign'], $value); } return $value; } } if ((defined('TESTS_ZEND_LOCALE_BCMATH_ENABLED') && !TESTS_ZEND_LOCALE_BCMATH_ENABLED) || !extension_loaded('bcmath')) { require_once 'Zend/Locale/Math/PhpMath.php'; }