Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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
Mark Story markstory authored
2  cake/libs/model/datasources/dbo/dbo_mysql.php
View
@@ -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 &&
3  cake/libs/model/datasources/dbo/dbo_mysqli.php
View
@@ -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)) {
19 cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php
View
@@ -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.