Skip to content

Commit 01501b6

Browse files
deekthesqueakPowerKiKi
authored andcommitted
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.
1 parent 9fa8a02 commit 01501b6

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
- Remove locale from formatting string - [#644](https://github.com/PHPOffice/PhpSpreadsheet/pull/644)
11+
12+
1013
### Fixed
1114

1215
- Allow iterators to go out of bounds with prev - [#587](https://github.com/PHPOffice/PhpSpreadsheet/issues/587)

src/PhpSpreadsheet/Style/NumberFormat.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,9 @@ public static function toFormattedString($value, $format, $callBack = null)
691691
// Strip #
692692
$format = preg_replace('/\\#/', '0', $format);
693693

694+
// Remove locale code [$-###]
695+
$format = preg_replace('/\[\$\-.*\]/', '', $format);
696+
694697
$n = '/\\[[^\\]]+\\]/';
695698
$m = preg_replace($n, '', $format);
696699
$number_regex = '/(0+)(\\.?)(0*)/';

tests/data/Style/NumberFormat.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,24 @@
186186
-1234567.8899999999,
187187
'0000:00.00',
188188
],
189+
[
190+
'18.952',
191+
18.952,
192+
'[$-409]General',
193+
],
194+
[
195+
'9.98',
196+
9.98,
197+
'[$-409]#,##0.00;-#,##0.00',
198+
],
199+
[
200+
'18.952',
201+
18.952,
202+
'[$-1010409]General',
203+
],
204+
[
205+
'9.98',
206+
9.98,
207+
'[$-1010409]#,##0.00;-#,##0.00',
208+
],
189209
];

tests/data/Style/NumberFormatDates.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,14 @@
6262
43270.603472222,
6363
'hh:mm:ss\ AM/PM',
6464
],
65+
[
66+
'8/20/2018',
67+
43332,
68+
'[$-409]m/d/yyyy',
69+
],
70+
[
71+
'8/20/2018',
72+
43332,
73+
'[$-1010409]m/d/yyyy',
74+
],
6575
];

0 commit comments

Comments
 (0)