php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42658 floats not converted to ints correctly
Submitted: 2007-09-13 14:06 UTC Modified: 2007-09-18 11:01 UTC
From: contax at phrenetic dot org Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.4 OS: Darwin
Private report: No CVE-ID: None
 [2007-09-13 14:06 UTC] contax at phrenetic dot org
Description:
------------
When converting specific numbers to integers via strings things seem to go a little wrong. (I have also tested this on 5.2.0 on both FreeBSD and Debian)

Reproduce code:
---------------
<?php
$no1 = '18.74';
$no2 = $no1 * 100;
$no3 = (int) $no2;
$no4 = intval($no2);
var_dump($no1);
var_dump($no2);
var_dump($no3);
var_dump($no4);
?>

Expected result:
----------------
string(5) "18.74"
float(1874)
int(1874)
int(1874)

Actual result:
--------------
string(5) "18.74"
float(1874)
int(1873)
int(1873)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-18 11:01 UTC] jani@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://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.


 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Sep 30 07:01:23 2020 UTC