From 454c01be51512bd19e28199dae54a743a534b21a Mon Sep 17 00:00:00 2001 From: Sebastian Nohn Date: Sat, 5 Feb 2022 21:46:50 +0100 Subject: [PATCH] Add support for one digit decimals (FORMAT_NUMBER_0, FORMAT_PERCENTAGE_0) (#2525) * Add support for one digit decimals (FORMAT_NUMBER_0) * Add support for one digit decimals (FORMAT_NUMBER_0, FORMAT_PERCENTAGE_0) * adding tests for one digit numbers * cleanup * add failing test to block merge of this PR until #2555 has been merged * fix code style * fix test --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Style/NumberFormat.php | 2 + tests/data/Style/NumberFormat.php | 115 ++++++++++++++++++++++ 3 files changed, 118 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9181e88c..c0a251ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Full support of the above CF Rules for the Xlsx Reader and Writer; even when the file being loaded has CF rules listed in the `` element for the worksheet rather than the `` element. - Provision of a CellMatcher to identify if rules are matched for a cell, and which matching style will be applied. - Improved documentation and examples, covering all supported CF rule types. + - Add support for one digit decimals (FORMAT_NUMBER_0, FORMAT_PERCENTAGE_0) ### Changed diff --git a/src/PhpSpreadsheet/Style/NumberFormat.php b/src/PhpSpreadsheet/Style/NumberFormat.php index 536b1d54..6f552cb3 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat.php +++ b/src/PhpSpreadsheet/Style/NumberFormat.php @@ -10,11 +10,13 @@ class NumberFormat extends Supervisor const FORMAT_TEXT = '@'; const FORMAT_NUMBER = '0'; + const FORMAT_NUMBER_0 = '0.0'; const FORMAT_NUMBER_00 = '0.00'; const FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00'; const FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-'; const FORMAT_PERCENTAGE = '0%'; + const FORMAT_PERCENTAGE_0 = '0.0%'; const FORMAT_PERCENTAGE_00 = '0.00%'; const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd'; diff --git a/tests/data/Style/NumberFormat.php b/tests/data/Style/NumberFormat.php index 520f790d..80f7080b 100644 --- a/tests/data/Style/NumberFormat.php +++ b/tests/data/Style/NumberFormat.php @@ -591,6 +591,61 @@ return [ '1.9', NumberFormat::FORMAT_NUMBER, ], + [ + '1.0', + '1.000', + NumberFormat::FORMAT_NUMBER_0, + ], + [ + '-1.0', + '-1.000', + NumberFormat::FORMAT_NUMBER_0, + ], + [ + '1.0', + '1', + NumberFormat::FORMAT_NUMBER_0, + ], + [ + '-1.0', + '-1', + NumberFormat::FORMAT_NUMBER_0, + ], + [ + '1.0', + '1', + NumberFormat::FORMAT_NUMBER_0, + ], + [ + '0.0', + '0', + NumberFormat::FORMAT_NUMBER_0, + ], + [ + '0.0', + '-0', + NumberFormat::FORMAT_NUMBER_0, + ], + [ + '1.1', + '1.11', + NumberFormat::FORMAT_NUMBER_0, + ], + [ + '1.1', + '1.14', + NumberFormat::FORMAT_NUMBER_0, + ], + [ + '1.2', + '1.15', + NumberFormat::FORMAT_NUMBER_0, + ], + [ + '1.2', + '1.19', + NumberFormat::FORMAT_NUMBER_0, + ], [ '0.00', '0', @@ -831,6 +886,66 @@ return [ '-0.019', NumberFormat::FORMAT_PERCENTAGE, ], + [ + '0.0%', + '0', + NumberFormat::FORMAT_PERCENTAGE_0, + ], + [ + '1.0%', + '0.01', + NumberFormat::FORMAT_PERCENTAGE_0, + ], + [ + '1.1%', + '0.011', + NumberFormat::FORMAT_PERCENTAGE_0, + ], + [ + '1.1%', + '0.0114', + NumberFormat::FORMAT_PERCENTAGE_0, + ], + [ + '1.2%', + '0.0115', + NumberFormat::FORMAT_PERCENTAGE_0, + ], + [ + '1.2%', + '0.0119', + NumberFormat::FORMAT_PERCENTAGE_0, + ], + [ + '0.0%', + '-0', + NumberFormat::FORMAT_PERCENTAGE_0, + ], + [ + '-1.0%', + '-0.01', + NumberFormat::FORMAT_PERCENTAGE_0, + ], + [ + '-1.1%', + '-0.011', + NumberFormat::FORMAT_PERCENTAGE_0, + ], + [ + '-1.1%', + '-0.0114', + NumberFormat::FORMAT_PERCENTAGE_0, + ], + [ + '-1.2%', + '-0.0115', + NumberFormat::FORMAT_PERCENTAGE_0, + ], + [ + '-1.2%', + '-0.0119', + NumberFormat::FORMAT_PERCENTAGE_0, + ], [ '0.00%', '0',