php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63684 pdo sqlite round function can't work correct
Submitted: 2012-12-04 08:40 UTC Modified: 2012-12-04 08:43 UTC
From: roastduck at qq dot com Assigned:
Status: Not a bug Package: PDO related
PHP Version: 5.3.19 OS: windows xp
Private report: No CVE-ID: None
 [2012-12-04 08:40 UTC] roastduck at qq dot com
Description:
------------
when I used pdo for the sql "select round(863521.777777778, 2) as a", it gets 
wrong result 863521.780000001

Test script:
---------------
<?php
$db = new PDO("sqlite:../test.s3db");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$st = $db->query('select round(863521.777777778, 2) as a');
$data = $st->fetchAll();
print_r($data);

$st = $db->query('select round(372366.577777778, 2) as a');
$data = $st->fetchAll();
print_r($data);


Expected result:
----------------
Array
(
    [0] => Array
        (
            [a] => 863521.780000001
            [0] => 863521.780000001
        )

)
Array
(
    [0] => Array
        (
            [a] => 372366.58
            [0] => 372366.58
        )

)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-04 08:43 UTC] aharvey@php.net
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://www.floating-point-gui.de/

Thank you for your interest in PHP.


 [2012-12-04 08:43 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 05:01:27 2024 UTC