Skip to content

Commit 0794764

Browse files
committed
ITST-17897: Leading/trailing zeroes are removed - Fixes 01: Detect "general" format correctly.
- Fix misinterpreting the style id as the number format id in formatValue(). - Add basic test for general format. - Adjust in-code doc.
1 parent 6f63041 commit 0794764

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

lib/NumberFormat.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ public function tryFormatValue($value, $xf_id)
235235
return $this->formatValue($value, $xf_id);
236236
}
237237

238-
/* Do not format value, either because quotePrefix is set ($xf_id = null),
239-
* or because "general" format should be applied ($xf_id = 0), which just outputs the value as-is. */
238+
/* Do not format value, no style set or quotePrefix is set. ($xf_id = null, starts at 1) */
240239
return $value;
241240
}
242241

@@ -289,15 +288,17 @@ private function convertNumberToDateTime($value)
289288
*/
290289
public function formatValue($value, $xf_id)
291290
{
291+
$num_fmt_id = 0;
292292
if (isset($this->xf_num_fmt_ids[$xf_id]) && $this->xf_num_fmt_ids[$xf_id] !== null) {
293-
$xf_id = $this->xf_num_fmt_ids[$xf_id];
294-
} else {
295-
// Invalid format_index or the style was explicitly declared as "don't format anything".
293+
$num_fmt_id = $this->xf_num_fmt_ids[$xf_id];
294+
}
295+
if ($num_fmt_id === 0) {
296+
// Invalid format_index or "general" format should be applied ($num_fmt_id = 0), which just outputs the value as-is.
296297
return $value;
297298
}
298299

299300
// Get definition of format for the given format_index.
300-
$section = $this->getFormatSectionForValue($value, $xf_id);
301+
$section = $this->getFormatSectionForValue($value, $num_fmt_id);
301302

302303
// If percentage values are expected, multiply value accordingly before formatting.
303304
if ($section->isPercentage()) {
@@ -333,7 +334,7 @@ public function formatValue($value, $xf_id)
333334
break;
334335
default:
335336
// Note: Should never happen. Exception is just to be safe.
336-
throw new RuntimeException('Specific datetime_type for format_index [' . $xf_id . '] is unknown.');
337+
throw new RuntimeException('Specific datetime_type for format_index [' . $num_fmt_id . '] is unknown.');
337338
}
338339
}
339340

tests/CustomNumberFormatTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,41 @@
1111
/** Check whether number formatting works properly for all sorts of custom formats. */
1212
class CustomNumberFormatTest extends TestCase
1313
{
14+
/**
15+
* Tests if the "general" format is correctly identified and applied.
16+
*
17+
* @dataProvider provideValuesForGeneralFormat
18+
*
19+
* @param string $value
20+
*
21+
* @throws Exception
22+
*/
23+
public function testGeneralFormat($value)
24+
{
25+
$cell_format = new NumberFormat();
26+
$cell_format->injectXfNumFmtIds(
27+
array(456 => 0) // Fake xf
28+
);
29+
$actual_output = $cell_format->formatValue($value, 456); // Apply numFmt of fake xf
30+
self::assertSame(
31+
$value, // General format should never change anything about the input value.
32+
$actual_output,
33+
'Unexpected formatting result for value [' . $value . '] with general format (number format 0).'
34+
);
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
public function provideValuesForGeneralFormat()
41+
{
42+
return array(
43+
'number' => array('123'),
44+
'decimal' => array('123.45'),
45+
'string' => array('abc')
46+
);
47+
}
48+
1449
/**
1550
* @dataProvider provideFormats
1651
*

0 commit comments

Comments
 (0)