php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22240 number_format problem with numbers > 999
Submitted: 2003-02-16 03:44 UTC Modified: 2003-02-18 07:17 UTC
From: shadlej at iwakuni dot usmc dot mil Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.3.0 OS: Windows
Private report: No CVE-ID: None
 [2003-02-16 03:44 UTC] shadlej at iwakuni dot usmc dot mil
I got this code from a book. It works as long as the total is less than 999. when it exceeds 999, it can't calculate the total properly. I think it has something to do with the number_function.  


+++++++++++++++++++++index.html ++++++++++++++++++

<form action="processorder.php" method=post>
<table border=0>

<tr bgcolor=#cccccc>
  <td width=150>Item</td>
  <td width=15>Quantity</td>
</tr>

<tr>
  <td>Tires</td>
  <td align=center><input type="text" name="tireqty" size=3 maxlength=3></td>
</tr>

<tr>
  <td>Oil</td>
  <td align=center><input type="text" name="oilqty" size=3 maxlength=3></td>
</tr>

<tr>
  <td>Spark Plugs</td>
  <td align=center><input type="text" name="sparkqty" size=3 maxlength=3></td>
</tr>

<tr>
  <td colspan=2 align=center><input type=submit value="Submit Order"></td>
</tr>
</table>
</form>


++++++++++++ preprocess.php ++++++++++++++++++

<html>
<head>
  <title>Jim's Auto Parts - Order Results</title>
</head>
<body>
<h1>Jim's Auto Parts</h1>
<h2>Order Results</h2>

<?
  define("TIREPRICE", 100);
  define("OILPRICE", 10);
  define("SPARKPRICE", 4);
  
  
  echo "<p>Order Processed at ";
  echo date("H:i, jS F");
  echo "<br>";
  echo "<p>Your order is as follows:";
  echo "<br>";
  echo $tireqty." tires<br>";
  echo $oilqty." bottles of oil<br>";
  echo $sparkqty." spark plugs<br>";
  
  $totalqty = $tireqty + $oilqty + $sparkqty;
  $totalamount = $tireqty  * TIREPRICE
               + $oilqty   * OILPRICE
	       + $sparkqty * SPARKPRICE;
  $totalamount = number_format($totalamount,2);
  echo "<br>\n";
  echo "Items Ordered:      ".$totalqty."<br>\n";
  echo "Subtotal:           $".$totalamount."<br>\n";
  $taxrate = 0.10;
  $totalamount = $totalamount * (1 + $taxrate);
  $totalamount = number_format($totalamount, 2);
  echo "Total including tax: $".$totalamount."<br>\n";

?>

</body>
</html>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-17 02:58 UTC] shadlej at iwakuni dot usmc dot mil
Same results. ( I used the Windows build ) Anything over 999 as a total gets an incorrect result. BTW number_function should read number_format. Sorry for the mistake.
 [2003-02-17 17:44 UTC] sniper@php.net
<?php

echo number_format(1111, 2);

?>

Does this work?

 [2003-02-18 05:27 UTC] shadlej at iwakuni dot usmc dot mil
Well, it sort-of worked. I had to move the number_format function off the $var and into the echo in BOTH areas. (follows) I don't know why it works the other way up until a total of 999 and anything after that is incorrect. I guess it doesn't matter. Lesson learned.  ThanX for following up. 


// $totalamount = number_format($totalamount, 2);
  echo "<br>\n";
  echo "Items Ordered:      ".$totalqty."<br>\n";
  echo "Subtotal:           $".number_format($totalamount, 2)."<br>\n";
  $taxrate = 0.10;
  $totalamount = $totalamount * (1 + $taxrate);
//  $totalamount = number_format($totalamount, 2);
  echo "Total including tax: $".number_format($totalamount, 2)."<br>\n";
 [2003-02-18 07:17 UTC] mgf@php.net
Just for the record, this was complete user-error.  The relevant lines from the originally posted script are:

  $totalamount = number_format($totalamount,2);
  ...
  $taxrate = 0.10;
  $totalamount = $totalamount * (1 + $taxrate);

The first quoted line will set $totalamount to a string representation of the dollar amount: "0.00" to "999.99" for numbers under 1000 (which is fine), but then "1,000.00" and so on for greater values.  This is then fed back into the calculation on the last quoted line, but as PHP doesn't recognize commas as a valid part of a number, you just get the digits before the first comma converted and used in your with-tax calculation.  This is why moving the number_format() into the echos is, in fact, the expected and correct solution.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Dec 14 07:01:24 2019 UTC