From 617ea476c08d937520f4b96d0715b0a840b7e7ef Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Wed, 30 Oct 2019 22:54:06 +0100 Subject: [PATCH] Complex number mask for integers containing a single dot (#1227) --- src/PhpSpreadsheet/Style/NumberFormat.php | 4 ++++ tests/data/Style/NumberFormat.php | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/PhpSpreadsheet/Style/NumberFormat.php b/src/PhpSpreadsheet/Style/NumberFormat.php index 6577aeff..7efa759e 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat.php +++ b/src/PhpSpreadsheet/Style/NumberFormat.php @@ -601,6 +601,7 @@ class NumberFormat extends Supervisor { $sign = ($number < 0.0); $number = abs($number); + if ($splitOnPoint && strpos($mask, '.') !== false && strpos($number, '.') !== false) { $numbers = explode('.', $number); $masks = explode('.', $mask); @@ -639,6 +640,9 @@ class NumberFormat extends Supervisor // Scientific format $value = sprintf('%5.2E', $value); } elseif (preg_match('/0([^\d\.]+)0/', $format) || substr_count($format, '.') > 1) { + if ($value == (int) $value && substr_count($format, '.') === 1) { + $value *= 10 ** strlen(explode('.', $format)[1]); + } $value = self::complexNumberFormatMask($value, $format); } else { $sprintf_pattern = "%0$minWidth." . strlen($right) . 'f'; diff --git a/tests/data/Style/NumberFormat.php b/tests/data/Style/NumberFormat.php index b7c39b3a..d9ab0940 100644 --- a/tests/data/Style/NumberFormat.php +++ b/tests/data/Style/NumberFormat.php @@ -58,6 +58,16 @@ return [ 5.5555000000000003, '0.00#', ], + [ + '12 345.67', + 12345.67, + '#\ ##0.00', + ], + [ + '1234 567.00', + 1234567.00, + '#\ ##0.00', + ], [ '5.556', 5.5555000000000003,