public function testSub() { Zend_Locale_Math_PhpMath::disable(); $this->assertEquals(-1, Zend_Locale_Math_PhpMath::Sub(1, 2)); $this->assertEquals(-2, Zend_Locale_Math_PhpMath::Sub(null, 2)); /** * BCMath extension doesn't actually operatest with a scientific notation (e.g. 1.2e+100) * So we shouldn't test numbers such as -9E+100, but probably should care about correct * float => string conversion * * @todo provide correct behavior */ // $this->assertEquals(9E+300, Zend_Locale_Math_PhpMath::Sub(-9E+100, -9E+300)); $this->assertEquals(5.89, Zend_Locale_Math_PhpMath::Sub(10.4444, 4.5556, 2)); $this->assertEquals(6, Zend_Locale_Math_PhpMath::Sub(10.4444, 4.5556, 0)); $this->assertEquals(-6, Zend_Locale_Math_PhpMath::Sub(-10.4444, -4.5556, 0)); $this->assertEquals(-1, Zend_Locale_Math_PhpMath::Sub(10, 11, 2)); }
/** * Surprisingly, the results of this implementation of round() * prove better than the native PHP round(). For example, try: * round(639.795, 2); * round(267.835, 2); * round(0.302515, 5); * round(0.36665, 4); * then try: * Zend_Locale_Math::round('639.795', 2); */ public static function round($op1, $precision = 0) { if (self::$_bcmathDisabled) { $op1 = round($op1, $precision); if (strpos((string) $op1, 'E') === false) { return self::normalize(round($op1, $precision)); } } if (strpos($op1, 'E') !== false) { $op1 = self::floatalize($op1); } $op1 = trim(self::normalize($op1)); $length = strlen($op1); if (($decPos = strpos($op1, '.')) === false) { $op1 .= '.0'; $decPos = $length; $length += 2; } if ($precision < 0 && abs($precision) > $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'; if ($op1 > 0) { if (self::$_bcmathDisabled) { return Zend_Locale_Math_PhpMath::Add($op1, $roundUp, $precision); } return self::Add($op1, $roundUp, $precision); } else { if (self::$_bcmathDisabled) { return Zend_Locale_Math_PhpMath::Sub($op1, $roundUp, $precision); } return self::Sub($op1, $roundUp, $precision); } } elseif ($precision >= 0) { return substr($op1, 0, $decPos + ($precision ? $precision + 1 : 0)); } return (string) $op1; }
public function testSub() { Zend_Locale_Math_PhpMath::disable(); $this->assertEquals(-1, Zend_Locale_Math_PhpMath::Sub(1, 2)); $this->assertEquals(-2, Zend_Locale_Math_PhpMath::Sub(null, 2)); try { $this->assertEquals(0, Zend_Locale_Math_PhpMath::Sub(-9.000000000000001E+100, -8.999999999999999E+200)); $this->fail("exception expected"); } catch (Zend_Locale_Math_Exception $e) { // success } $this->assertEquals(5.89, Zend_Locale_Math_PhpMath::Sub(10.4444, 4.5556, 2)); $this->assertEquals(6, Zend_Locale_Math_PhpMath::Sub(10.4444, 4.5556, 0)); $this->assertEquals(-6, Zend_Locale_Math_PhpMath::Sub(-10.4444, -4.5556, 0)); $this->assertEquals(-1, Zend_Locale_Math_PhpMath::Sub(10, 11, 2)); }