mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-12 02:56:44 +00:00
Merge pull request #3903 from oleibman/issue3900
Handling of User-supplied Decimal and Thousands Separators
This commit is contained in:
@@ -17,7 +17,7 @@ abstract class BaseFormatter
|
||||
$thousandsSeparator = StringHelper::getThousandsSeparator();
|
||||
$decimalSeparator = StringHelper::getDecimalSeparator();
|
||||
if ($thousandsSeparator !== ',' || $decimalSeparator !== '.') {
|
||||
$value = str_replace(['.', ',', "\u{fffd}"], ["\u{fffd}", '.', ','], $value);
|
||||
$value = str_replace(['.', ',', "\u{fffd}"], ["\u{fffd}", $thousandsSeparator, $decimalSeparator], $value);
|
||||
}
|
||||
|
||||
return $value;
|
||||
|
||||
@@ -5,6 +5,8 @@ declare(strict_types=1);
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Shared;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Csv;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class StringHelperTest extends TestCase
|
||||
@@ -98,4 +100,24 @@ class StringHelperTest extends TestCase
|
||||
|
||||
self::assertEquals($expectedResult, $result);
|
||||
}
|
||||
|
||||
public function testIssue3900(): void
|
||||
{
|
||||
StringHelper::setDecimalSeparator('.');
|
||||
StringHelper::setThousandsSeparator('');
|
||||
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$sheet->setCellValue('A1', 1.4);
|
||||
$sheet->setCellValue('B1', 1004.5);
|
||||
$sheet->setCellValue('C1', 1000000.5);
|
||||
|
||||
ob_start();
|
||||
$ioWriter = new Csv($spreadsheet);
|
||||
$ioWriter->setDelimiter(';');
|
||||
$ioWriter->save('php://output');
|
||||
$output = ob_get_clean();
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
self::assertSame('"1.4";"1004.5";"1000000.5"' . PHP_EOL, $output);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user