Floats must not be stored as locale dependent
Floats are currently stored formatted per the locale setting. This leads to different values being written whether the locale uses "." or "," for the decimal point for instance. This poses a problem as floats must be stored using "." as the decimal point to be valid. This commit ensures that the floats are stored correctly by forcing the formatting of the value.
This commit is contained in:
1 parent
eb84ec9364
commit
8c1f0cc447
5 files changed
+130
-3
No files matched your search
@@ -279,6 +279,52 @@ class WriterTest extends TestCase
|
||||
$this->assertValueWasWrittenToSheet($fileName, 1, 10.2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddRowShouldSupportFloatValuesInDifferentLocale()
|
||||
{
|
||||
$previousLocale = \setlocale(LC_ALL, 0);
|
||||
|
||||
try {
|
||||
// Pick a supported locale whose decimal point is a comma.
|
||||
// Installed locales differ from one system to another, so we can't pick
|
||||
// a given locale.
|
||||
$supportedLocales = explode("\n", shell_exec('locale -a'));
|
||||
foreach ($supportedLocales as $supportedLocale) {
|
||||
\setlocale(LC_ALL, $supportedLocale);
|
||||
if (\localeconv()['decimal_point'] === ',') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->assertEquals(',', \localeconv()['decimal_point']);
|
||||
|
||||
$cellValue = 1234.5;
|
||||
var_dump("Cell value before: " . $cellValue);
|
||||
$cellValue = str_replace(
|
||||
[\localeconv()['thousands_sep'], \localeconv()['decimal_point']],
|
||||
['', '.'],
|
||||
$cellValue
|
||||
);
|
||||
var_dump("Cell value after: " . $cellValue);
|
||||
var_dump("Thousands sep: " . \localeconv()['thousands_sep']);
|
||||
var_dump("Decimal point: " . \localeconv()['decimal_point']);
|
||||
|
||||
$fileName = 'test_add_row_should_support_float_values_in_different_locale.xlsx';
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
[1234.5],
|
||||
]);
|
||||
|
||||
$this->writeToODSFile($dataRows, $fileName);
|
||||
|
||||
$this->assertValueWasNotWrittenToSheet($fileName, 1, "1234,5");
|
||||
$this->assertValueWasWrittenToSheet($fileName, 1, "1234.5");
|
||||
} finally {
|
||||
// reset locale
|
||||
\setlocale(LC_ALL, $previousLocale);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user