Page 1 of 1

getting wrong figures from script (RESOLVED!)

Posted: Sat Jan 08, 2011 11:13 am
by DelPazzo
Hi,

am running a script but with 2 variables I get 2 different figures for what should be the same.

Code: Select all

         while (!$recordSet3->EOF) {
                $auftrag_id = $recordSet3->fields['auftrag_id'];
                $text = $recordSet3->fields['text'];
                $preis = $recordSet3->fields['preis'];
                $total = $total+$preis;
                $total = number_format($total,2);
                echo '/'.$total;
                $preis = number_format($preis,2);
                $preis = str_replace(",",".",$preis);
                $preis = str_replace(".",",",$preis);
                $counter = $counter+1;
                $pdf->Cell(60,0,$auftrag_id,0,0,'L');
                $pdf->Cell(60,0,$text,0,0,'L');
                $pdf->Cell(30,0,$wpreis,0,0,'R');
                $pdf->Cell(30,0,$preis.' '.$currenc,0,0,'R');
                $pdf->Ln(4);
                $recordSet3->MoveNext();
         }
$preis = 64,55 which is correct but $total gives back 65,00 and I don't know why. Did I miss an error?

Re: getting wrong figures from script

Posted: Sat Jan 08, 2011 1:28 pm
by califdon
This is a while loop--it is stepping through all the records of the recordset--and $total is accumulating the $total of the current and all previous $preis values. So what do you mean that the 2 variables should be the same? Are you saying that they are not the same on the very first record? You should initialize $total to zero before the while loop begins.

Re: getting wrong figures from script

Posted: Sat Jan 08, 2011 1:41 pm
by DelPazzo
sorry yes forgot that part. it's already in the 1st loop that there is a difference and yes I set total to zero before the loop.

Re: getting wrong figures from script

Posted: Sat Jan 08, 2011 2:05 pm
by califdon
I don't see any reason for the behavior you describe. I just tried a little test and it doesn't round up the value as you described. The way I would try to debug it is to echo the values of both $preis and $total at several points, first before the loop begins, then immediately after each line that modifies the value of either variable. This will isolate where the process is generating the incorrect value.

Re: getting wrong figures from script

Posted: Sat Jan 08, 2011 2:26 pm
by DelPazzo
I tried already. before the loop it is 0, before the price is set it is 0 right after the price (64,66) is added it is 65

Re: getting wrong figures from script

Posted: Sat Jan 08, 2011 3:20 pm
by califdon
DelPazzo wrote:I tried already. before the loop it is 0, before the price is set it is 0 right after the price (64,66) is added it is 65
I would say that you have isolated the problem. Something is rounding up to the next whole number. Could it be that the field is an integer field?

Re: getting wrong figures from script

Posted: Sat Jan 08, 2011 3:25 pm
by DelPazzo
it's a money field. but what's also strange. i don't know if it's a standard feature of php. the $total is automatically set to no decimals.

here are the 3 lines before the do while

Code: Select all

         $total = 0;
         $mwsttot = 0;
         $counter = 0;

Re: getting wrong figures from script

Posted: Sat Jan 08, 2011 3:45 pm
by califdon
What database are you using? MySQL doesn't have a 'money' datatype, although I think MS SQL Server does. In any case, that's probably your answer. That must be changed to permit 2 decimals.

Re: getting wrong figures from script

Posted: Sat Jan 08, 2011 3:46 pm
by DelPazzo
it's ms sql and money type means 4 decimals, otherwise it would not get the price with 64,66, that would then be 65 too

Re: getting wrong figures from script

Posted: Sat Jan 08, 2011 6:52 pm
by Neilos
DelPazzo wrote:it's ms sql and money type means 4 decimals, otherwise it would not get the price with 64,66, that would then be 65 too
decimal places, as in $123456.95 has two decimal places, so does $234.00 and $0.01.

four decimal places would surely be $64.6600?

Try multiplying 64,66 by 100 and anything u add to it, then sum them and divide $total by 100 before you echo it. This is not a long term solution but it will let you see if there is a decimal point problem at least if the answer comes out correctly.

Re: getting wrong figures from script

Posted: Sun Jan 09, 2011 4:32 am
by DelPazzo
if i multiply preis by 100 I get 6500

edit: ok, after several tries it seems to happen as soon as I want to calculate (add, multiply, divide, subtact...) with preis or whatever I call the variable

Re: getting wrong figures from script

Posted: Sun Jan 09, 2011 4:40 am
by adamdressller81
thank you califdon for yout help and support.

Re: getting wrong figures from script

Posted: Sun Jan 09, 2011 4:48 am
by DelPazzo
adamdressller81 wrote:thank you califdon for yout help and support.
???

Re: getting wrong figures from script

Posted: Sun Jan 09, 2011 7:46 am
by DelPazzo
got it working! :)

I just needed to change the order of the lines:

Code: Select all

         while (!$recordSet3->EOF) {
                $auftrag_id = $recordSet3->fields['auftrag_id'];
                $text = $recordSet3->fields['text'];
                $preis = $recordSet3->fields['preis'];
                $preis = $preis;
                $preis = number_format($preis,2); // <- this one move before the summing line
                $total = $total+$preis;
                echo '/'.$preis;
                echo '/'.$total;
                $total = number_format($total,2);
                $preis = str_replace(",",".",$preis);
                $preis = str_replace(".",",",$preis);
                $counter = $counter+1;
                $pdf->Cell(60,0,$auftrag_id,0,0,'L');
                $pdf->Cell(60,0,$text,0,0,'L');
                $pdf->Cell(30,0,$wpreis,0,0,'R');
                $pdf->Cell(30,0,$preis.' '.$currenc,0,0,'R');
                $pdf->Ln(4);
                $recordSet3->MoveNext();
         }
doing this is asp would cause probs with number formatting... that's why I did not think of it.