Re: sprintf problem... (with simplified code )

From: Date: Sat, 22 Mar 2008 21:35:12 +0000
Subject: Re: sprintf problem... (with simplified code )
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-271992@lists.php.net to get a copy of this message
Here is a simplifie version of my code : <?php $total = 0; $ht = 10.03; $vat = 19.6; $total += $ht*($vat/100+1); print("Total = ".sprintf("%0.2f", $total)); $total += $ht*($vat/100+1); print(" - Total 2 = ".sprintf("%0.2f", $total)); ?> I get 12.00 and 23.99 ... what should I do to get 12.00 and 24.00 ? Richard a écrit :
Hello, I'm trying to write a invoice script but I have encountered a slight problem. Each product needs to be listed without tax, and at the end of the file I need to show the VAT. In France VAT is 19.6% So €10.03 without vat = €12.00 with vat. So I do this : $totalwithoutvat = 0; $totalwithoutvat = 0; $totalvat =0; while ( $itemwithoutvat) = each( $px ) ) { $totalwithoutvat += $itemwithoutvat; $totalwithoutvat += sprintf("%0.2f", $itemwithoutvat * ( 1 + 19.6/100 )); $totalvat += sprintf("%0.2f",$itemwithoutvat * 19.6/100); } print ("Total Vat =".sprintf("%0.2f",$totalvat)." - Total without vat = ".sprintf("%0.2f",$totalwithoutvat)." - Total with vat = ".sprintf("%0.2f",$totalwithvat)); But I'm not sure I am using sprintf correctly, maybe I should use a different function because : When I have one item at 10.03, the result is : Total Vat = 1.97 - Total without vat = 10.03 Total with vat = 12.00 but if I have two items at 10.03 the result is : Total vat = 3.93 - Total without vat = 20.06 Total with vat = 23.99 but I need it to be : Total vat = 3.94 - Total without vat = 20.06 Total with vat = 24.00 So from what I can see, sprintf only seems to work for printing and the actual result is kept in memory, is this correct ? if so what function should I use instead? Thanks in advance :) Richard


« previous php.general (#271992) next »