Skip to content

Commit d0393a2

Browse files
authored
Merge pull request #3903 from oleibman/issue3900
Handling of User-supplied Decimal and Thousands Separators
2 parents 0c8a80e + e1fb68e commit d0393a2

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)