Skip to content

Commit e1fb68e

Browse files
committed
Handling of User-supplied Decimal and Thousands Separators
Fix #3900. The code to adjust Decimal and Thousands separators to user-specified choices expects to convert periods to commas and vice versa - hard-coded without taking the user specification into account. This is likely to be the only significant use case, however, as the issue demonstrates, it is not the only possibility. In particular, it will mess up Csv output if decimal separator is kept as period, but thousands separator is set to null string. The code is altered to replace period with the user-selected decimal separator, and comma with the user-selected thousands separator. No existing tests broke as a result of this change in behavior.
1 parent d620497 commit e1fb68e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/PhpSpreadsheet/Style/NumberFormat/BaseFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected static function adjustSeparators(string $value): string
1717
$thousandsSeparator = StringHelper::getThousandsSeparator();
1818
$decimalSeparator = StringHelper::getDecimalSeparator();
1919
if ($thousandsSeparator !== ',' || $decimalSeparator !== '.') {
20-
$value = str_replace(['.', ',', "\u{fffd}"], ["\u{fffd}", '.', ','], $value);
20+
$value = str_replace(['.', ',', "\u{fffd}"], ["\u{fffd}", $thousandsSeparator, $decimalSeparator], $value);
2121
}
2222

2323
return $value;

tests/PhpSpreadsheetTests/Shared/StringHelperTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace PhpOffice\PhpSpreadsheetTests\Shared;
66

77
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
8+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9+
use PhpOffice\PhpSpreadsheet\Writer\Csv;
810
use PHPUnit\Framework\TestCase;
911

1012
class StringHelperTest extends TestCase
@@ -98,4 +100,24 @@ public function testSYLKtoUTF8(): void
98100

99101
self::assertEquals($expectedResult, $result);
100102
}
103+
104+
public function testIssue3900(): void
105+
{
106+
StringHelper::setDecimalSeparator('.');
107+
StringHelper::setThousandsSeparator('');
108+
109+
$spreadsheet = new Spreadsheet();
110+
$sheet = $spreadsheet->getActiveSheet();
111+
$sheet->setCellValue('A1', 1.4);
112+
$sheet->setCellValue('B1', 1004.5);
113+
$sheet->setCellValue('C1', 1000000.5);
114+
115+
ob_start();
116+
$ioWriter = new Csv($spreadsheet);
117+
$ioWriter->setDelimiter(';');
118+
$ioWriter->save('php://output');
119+
$output = ob_get_clean();
120+
$spreadsheet->disconnectWorksheets();
121+
self::assertSame('"1.4";"1004.5";"1000000.5"' . PHP_EOL, $output);
122+
}
101123
}

0 commit comments

Comments
 (0)