From d778b41524b8967af37bc90af820c0db76d1711b Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Fri, 23 Jan 2015 15:40:54 +0000 Subject: [PATCH] Fix to negative number handling with complex number format masks, so that the sign only appears once --- Classes/PHPExcel/Style/NumberFormat.php | 17 +++++++++++------ unitTests/rawTestData/Style/NumberFormat.data | 1 + 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Classes/PHPExcel/Style/NumberFormat.php b/Classes/PHPExcel/Style/NumberFormat.php index e098f6f2..39e5dfab 100644 --- a/Classes/PHPExcel/Style/NumberFormat.php +++ b/Classes/PHPExcel/Style/NumberFormat.php @@ -498,12 +498,14 @@ class PHPExcel_Style_NumberFormat extends PHPExcel_Style_Supervisor implements P } } - private static function _complexNumberFormatMask($number, $mask) { + private static function _complexNumberFormatMask($number, $mask, $level = 0) { + $sign = ($number < 0.0); + $number = abs($number); if (strpos($mask,'.') !== false) { $numbers = explode('.', $number . '.0'); $masks = explode('.', $mask . '.0'); - $result1 = self::_complexNumberFormatMask($numbers[0], $masks[0]); - $result2 = strrev(self::_complexNumberFormatMask(strrev($numbers[1]), strrev($masks[1]))); + $result1 = self::_complexNumberFormatMask($numbers[0], $masks[0], 1); + $result2 = strrev(self::_complexNumberFormatMask(strrev($numbers[1]), strrev($masks[1])), 1); return $result1 . '.' . $result2; } @@ -521,7 +523,7 @@ class PHPExcel_Style_NumberFormat extends PHPExcel_Style_Supervisor implements P fmod($number, $divisor) ); $number = floor($number / $divisor); - $mask = substr_replace($mask,$blockValue, $offset, $size); + $mask = substr_replace($mask, $blockValue, $offset, $size); } if ($number > 0) { $mask = substr_replace($mask, $number, $offset, 0); @@ -531,7 +533,7 @@ class PHPExcel_Style_NumberFormat extends PHPExcel_Style_Supervisor implements P $result = $number; } - return $result; + return (($sign) ? '-' : '') . $result; } /** @@ -614,9 +616,12 @@ class PHPExcel_Style_NumberFormat extends PHPExcel_Style_Supervisor implements P // Some non-number characters are escaped with \, which we don't need $format = preg_replace("/\\\\/", '', $format); +// Handle escaped characters, such as \" to display a literal " or \\ to display a literal \ +// $format = preg_replace('/(?