Skip to content
Permalink
Browse files

Prefer locale parsing to basic float casting.

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 ee3ece0c8df3736d4991fc469e69a29a25fee0e6
Showing with 18 additions and 3 deletions.
  1. +3 −3 src/Database/Type/DecimalType.php
  2. +15 −0 tests/TestCase/Database/Type/DecimalTypeTest.php
@@ -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;
}
@@ -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.
*

0 comments on commit ee3ece0

Please sign in to comment.
You can’t perform that action at this time.