php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22712 number_format rounding differences for floats and strings
Submitted: 2003-03-14 14:32 UTC Modified: 2003-03-14 14:49 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: hpdl at oscommerce dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.3.1 OS: Linux
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: hpdl at oscommerce dot com
New email:
PHP Version: OS:

 

 [2003-03-14 14:32 UTC] hpdl at oscommerce dot com
We at osCommerce are witnessing rounding problems in our 
taxes logic and have made a reproduceable script below. 
 
number_format() produces different rounding results when 
floats or strings are passed to the function. 
 
<?php 
  $price = '100.0000'; 
 
// Canada GST 7% 
  $price = '1.07' * $price; 
 
// Canada/Quebec PST 7.5% 
  $price = '1.075' * $price; 
 
  echo '$price: ' . $price . '<br>' . "\n" . 
       'number_format($price, 2): ' . 
number_format($price, 2) . '<br>' . "\n" . 
       'number_format((string)$price, 2): ' . 
number_format((string)$price, 2); 
?> 
 
The correct results are produced when the string value is 
passed. The float value produces incorrect results. 
 
I do not know if this is a problem with the number_format 
function, or if it is due to floats/mathematics. 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-14 14:49 UTC] derick@php.net
It's a float/maths problem, nothing to do with PHP specific.

Derick
 [2003-08-07 13:23 UTC] q1234562001 at mail dot ru
The script:

<?php
      echo number_format(63.745, 2, '.', "") ."<br>";
      echo number_format(64.745, 2, '.', "") ."<br>";
?>

Result:

63.74
64.75
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 05 14:01:29 2024 UTC