Remove locale from format string to prevent formatting error (#644)

When a formatting string has a locale in it an error can occur when outputting. For example when the format string with a locale such as `[$-1010409]#,##0.00;-#,##0.00` appears, a value of 9.98 comes back as $9.98. This is because at https://github.com/PHPOffice/PhpSpreadsheet/blob/1.4.0/src/PhpSpreadsheet/Style/NumberFormat.php#L711 the numberFormat regex will match to the zeros inside the locale ([$-1010409]). Attempts to adjust the numberFormat regex caused regressions in other tests. Adding another step to filter out the locale caused no regression.
This commit is contained in:
Derek Bonner 2018-09-28 05:28:40 -07:00 committed by Adrien Crivelli
parent 9fa8a02fe3
commit 01501b6ff2
4 changed files with 36 additions and 0 deletions

View File

@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased] ## [Unreleased]
- Remove locale from formatting string - [#644](https://github.com/PHPOffice/PhpSpreadsheet/pull/644)
### Fixed ### Fixed
- Allow iterators to go out of bounds with prev - [#587](https://github.com/PHPOffice/PhpSpreadsheet/issues/587) - Allow iterators to go out of bounds with prev - [#587](https://github.com/PHPOffice/PhpSpreadsheet/issues/587)

View File

@ -691,6 +691,9 @@ class NumberFormat extends Supervisor
// Strip # // Strip #
$format = preg_replace('/\\#/', '0', $format); $format = preg_replace('/\\#/', '0', $format);
// Remove locale code [$-###]
$format = preg_replace('/\[\$\-.*\]/', '', $format);
$n = '/\\[[^\\]]+\\]/'; $n = '/\\[[^\\]]+\\]/';
$m = preg_replace($n, '', $format); $m = preg_replace($n, '', $format);
$number_regex = '/(0+)(\\.?)(0*)/'; $number_regex = '/(0+)(\\.?)(0*)/';

View File

@ -186,4 +186,24 @@ return [
-1234567.8899999999, -1234567.8899999999,
'0000:00.00', '0000:00.00',
], ],
[
'18.952',
18.952,
'[$-409]General',
],
[
'9.98',
9.98,
'[$-409]#,##0.00;-#,##0.00',
],
[
'18.952',
18.952,
'[$-1010409]General',
],
[
'9.98',
9.98,
'[$-1010409]#,##0.00;-#,##0.00',
],
]; ];

View File

@ -62,4 +62,14 @@ return [
43270.603472222, 43270.603472222,
'hh:mm:ss\ AM/PM', 'hh:mm:ss\ AM/PM',
], ],
[
'8/20/2018',
43332,
'[$-409]m/d/yyyy',
],
[
'8/20/2018',
43332,
'[$-1010409]m/d/yyyy',
],
]; ];