Fix to negative number handling with complex number format masks, so that the sign only appears once

This commit is contained in:
MarkBaker 2015-01-23 15:40:54 +00:00
parent dc137c293a
commit d778b41524
2 changed files with 12 additions and 6 deletions

View File

@ -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) { if (strpos($mask,'.') !== false) {
$numbers = explode('.', $number . '.0'); $numbers = explode('.', $number . '.0');
$masks = explode('.', $mask . '.0'); $masks = explode('.', $mask . '.0');
$result1 = self::_complexNumberFormatMask($numbers[0], $masks[0]); $result1 = self::_complexNumberFormatMask($numbers[0], $masks[0], 1);
$result2 = strrev(self::_complexNumberFormatMask(strrev($numbers[1]), strrev($masks[1]))); $result2 = strrev(self::_complexNumberFormatMask(strrev($numbers[1]), strrev($masks[1])), 1);
return $result1 . '.' . $result2; return $result1 . '.' . $result2;
} }
@ -521,7 +523,7 @@ class PHPExcel_Style_NumberFormat extends PHPExcel_Style_Supervisor implements P
fmod($number, $divisor) fmod($number, $divisor)
); );
$number = floor($number / $divisor); $number = floor($number / $divisor);
$mask = substr_replace($mask,$blockValue, $offset, $size); $mask = substr_replace($mask, $blockValue, $offset, $size);
} }
if ($number > 0) { if ($number > 0) {
$mask = substr_replace($mask, $number, $offset, 0); $mask = substr_replace($mask, $number, $offset, 0);
@ -531,7 +533,7 @@ class PHPExcel_Style_NumberFormat extends PHPExcel_Style_Supervisor implements P
$result = $number; $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 // Some non-number characters are escaped with \, which we don't need
$format = preg_replace("/\\\\/", '', $format); $format = preg_replace("/\\\\/", '', $format);
// Handle escaped characters, such as \" to display a literal " or \\ to display a literal \
// $format = preg_replace('/(?<!\\\\)\"/', '', $format);
// $format = str_replace(array('\\"', '*'), array('"', ''), $format);
// Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols // Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols
$format = str_replace(array('"','*'), '', $format); $format = str_replace(array('"', '*'), '', $format);
// Find out if we need thousands separator // Find out if we need thousands separator
// This is indicated by a comma enclosed by a digit placeholder: // This is indicated by a comma enclosed by a digit placeholder:

View File

@ -32,3 +32,4 @@
123456789, '(000) 0-0000-000', "(001) 2-3456-789" 123456789, '(000) 0-0000-000', "(001) 2-3456-789"
123456789, '0 (+00) 0000 00 00 00', "0 (+00) 0123 45 67 89" 123456789, '0 (+00) 0000 00 00 00', "0 (+00) 0123 45 67 89"
123456789, '0000:00:00', "12345:67:89" 123456789, '0000:00:00', "12345:67:89"
-123456789, '0000:00:00', "-12345:67:89"