php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19189 sprintf yields wrong result
Submitted: 2002-08-30 03:20 UTC Modified: 2002-08-30 04:47 UTC
From: gwenzel at emb dot de Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.2.0 OS: Linux Suse 7.1
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gwenzel at emb dot de
New email:
PHP Version: OS:

 

 [2002-08-30 03:20 UTC] gwenzel at emb dot de
echo ( sprintf("%012d", 68.85*100 ) );
yields 
000000006884

Strangly enough:
echo ( sprintf("%012d", 68.95*100 ) );
yields the correct result: 
000000006895


 './configure' '--with-apxs=/usr/local/apache/bin/apxs' '--enable-trans-sid' '--with-mysql' '--enable-ftp' '--with-config-file-path=/www/conf' '--with-zlib-dir=/usr/local' '--with-gd=/usr/local' '--with-png-dir=/usr/local' '--with-jpeg-dir=/usr/local' '--with-freetype-dir=/usr/local'

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-08-30 03:32 UTC] sander@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Use the bcmath extension to avoid problems like these.
 [2002-08-30 04:43 UTC] gwenzel at emb dot de
Ok. This might not be a bug in sprintf.

But why does 
echo ( 68.85*100 ) yield 6885
and
echo ( intval ( 68.85*100 ) ) yield 6884 ?

And by the way my provider does not support PHP with the bcmath extension
 [2002-08-30 04:43 UTC] gwenzel at emb dot de
Ok. This might not be a bug in sprintf.

But why does 
echo ( 68.85*100 ) yield 6885
and
echo ( intval ( 68.85*100 ) ) yield 6884 ?

And by the way my provider does not support PHP with the bcmath extension
 [2002-08-30 04:47 UTC] derick@php.net
Because 68.85 * 100 is about 6885 (perhaps it is 6884.999999999991).

See also:
http://www.php.net/manual/en/print/language.types.float.php

and maybe changing your precision helps:
http://www.php.net/manual/en/configuration.php#ini.precision

Derick
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC