sprintf problem...

From: Date: Sat, 22 Mar 2008 20:37:50 +0000
Subject: sprintf problem...
Groups: php.general 
Request: Send a blank email to php-general+get-271991@lists.php.net to get a copy of this message
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 (#271991) next »