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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: roastduck at qq dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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: Thu Dec 26 15:01:32 2024 UTC