Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Change how floats are formatted in MySQL.

Fixing precision issues where numbers would get truncated.
Also maintaining compatibility with scientific notation.
Fixes #2049
  • Loading branch information...
commit c143908a035112fbb17661a6555547d9e7a70487 1 parent a6e4208
@markstory markstory authored
View
2  cake/libs/model/datasources/dbo/dbo_mysql.php
@@ -672,7 +672,7 @@ function value($data, $column = null, $safe = false) {
return 'NULL';
}
if (is_float($data)) {
- return str_replace(',', '.', sprintf('%G', $data));
+ return str_replace(',', '.', strval($data));
}
if ((is_int($data) || $data === '0') || (
is_numeric($data) && strpos($data, ',') === false &&
View
3  cake/libs/model/datasources/dbo/dbo_mysqli.php
@@ -188,6 +188,9 @@ function value($data, $column = null, $safe = false) {
if ($data === '') {
return 'NULL';
}
+ if (is_float($data)) {
+ return str_replace(',', '.', strval($data));
+ }
if ((is_int($data) || is_float($data) || $data === '0') || (
is_numeric($data) && strpos($data, ',') === false &&
$data[0] != '0' && strpos($data, 'e') === false)) {
View
19 cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php
@@ -239,10 +239,22 @@ function testLocalizedFloats() {
setlocale(LC_ALL, 'de_DE');
$result = $this->db->value(3.141593, 'float');
- $this->assertTrue(strpos((string)$result, ',') === false);
+ $this->assertEqual('3.141593', $result);
$result = $this->db->value(3.141593);
- $this->assertTrue(strpos((string)$result, ',') === false);
+ $this->assertEqual('3.141593', $result);
+
+ $result = $this->db->value(3.141593);
+ $this->assertEqual('3.141593', $result);
+
+ $result = $this->db->value(1234567.11, 'float');
+ $this->assertEqual('1234567.11', $result);
+
+ $result = $this->db->value(123456.45464748, 'float');
+ $this->assertEqual('123456.454647', $result);
+
+ $result = $this->db->value(0.987654321, 'float');
+ $this->assertEqual('0.987654321', (string)$result);
$result = $this->db->value(2.2E-54, 'float');
$this->assertEqual('2.2E-54', (string)$result);
@@ -262,6 +274,9 @@ function testScientificNotation() {
$result = $this->db->value(2.2E-54, 'float');
$this->assertEqual('2.2E-54', (string)$result);
+ $result = $this->db->value(2.2E-54, 'float');
+ $this->assertEqual('2.2E-54', (string)$result);
+
$result = $this->db->value(2.2E-54);
$this->assertEqual('2.2E-54', (string)$result);
}
Please sign in to comment.
Something went wrong with that request. Please try again.