Skip to content

Commit

Permalink
Prefer locale parsing to basic float casting.
Browse files Browse the repository at this point in the history
Many european locales use `.` for thousands separator, which will result
in the wrong value if handled by a float cast.

Refs #11235
  • Loading branch information
markstory committed Sep 28, 2017
1 parent 9d6ceb6 commit ee3ece0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Database/Type/DecimalType.php
Expand Up @@ -129,12 +129,12 @@ public function marshal($value)
if ($value === null || $value === '') {
return null;
}
if (is_numeric($value)) {
return (float)$value;
}
if (is_string($value) && $this->_useLocaleParser) {
return $this->_parseValue($value);
}
if (is_numeric($value)) {
return (float)$value;
}
if (is_array($value)) {
return 1;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Database/Type/DecimalTypeTest.php
Expand Up @@ -181,6 +181,21 @@ public function testMarshalWithLocaleParsing()
$this->assertEquals($expected, $result);
}

/**
* test marshall() number in the danish locale which uses . for thousands separator.
*
* @return void
*/
public function testMarshallWithLocaleParsingDanish()
{
I18n::setLocale('da_DK');

$this->type->useLocaleParser();
$expected = 47500.0;
$result = $this->type->marshal('47.500');
$this->assertSame($expected, $result);
}

/**
* Test that exceptions are raised on invalid parsers.
*
Expand Down

0 comments on commit ee3ece0

Please sign in to comment.