From 42fa6f10a17b5978a2c83003e7848d9a5c16fc2e Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Mon, 30 Jan 2012 00:41:52 +0000 Subject: [PATCH] Minor tweaks to Excel functions to handle envelope cases git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@85866 2327b42d-5241-43d6-9e2a-de5ac946f064 --- Classes/PHPExcel/Calculation/MathTrig.php | 26 ++++++++++------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/Classes/PHPExcel/Calculation/MathTrig.php b/Classes/PHPExcel/Calculation/MathTrig.php index a3fd92eb..7d313740 100644 --- a/Classes/PHPExcel/Calculation/MathTrig.php +++ b/Classes/PHPExcel/Calculation/MathTrig.php @@ -1241,29 +1241,25 @@ class PHPExcel_Calculation_MathTrig { * Truncates value to the number of fractional digits by number_digits. * * @param float $value - * @param int $number_digits + * @param int $digits * @return float Truncated value */ - public static function TRUNC($value = 0, $number_digits = 0) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $number_digits = PHPExcel_Calculation_Functions::flattenSingleValue($number_digits); + public static function TRUNC($value = 0, $digits = 0) { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $digits = PHPExcel_Calculation_Functions::flattenSingleValue($digits); // Validate parameters - if ($number_digits < 0) { + if ((!is_numeric($value)) || (!is_numeric($digits))) return PHPExcel_Calculation_Functions::VALUE(); - } + $digits = floor($digits); // Truncate - if ($number_digits > 0) { - $value = $value * pow(10, $number_digits); - } - $value = intval($value); - if ($number_digits > 0) { - $value = $value / pow(10, $number_digits); - } + $adjust = pow(10, $digits); - // Return - return $value; + if (($digits > 0) && (rtrim(intval((abs($value) - abs(intval($value))) * $adjust),'0') < $adjust/10)) + return $value; + + return (intval($value * $adjust)) / $adjust; } // function TRUNC() } // class PHPExcel_Calculation_MathTrig