getting wrong figures from script (RESOLVED!)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DelPazzo
Forum Newbie
Posts: 23
Joined: Thu Dec 30, 2010 3:49 am

getting wrong figures from script (RESOLVED!)

Post 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?
Last edited by DelPazzo on Sun Jan 09, 2011 7:46 am, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: getting wrong figures from script

Post 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.
DelPazzo
Forum Newbie
Posts: 23
Joined: Thu Dec 30, 2010 3:49 am

Re: getting wrong figures from script

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: getting wrong figures from script

Post 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.
DelPazzo
Forum Newbie
Posts: 23
Joined: Thu Dec 30, 2010 3:49 am

Re: getting wrong figures from script

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: getting wrong figures from script

Post 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?
DelPazzo
Forum Newbie
Posts: 23
Joined: Thu Dec 30, 2010 3:49 am

Re: getting wrong figures from script

Post 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;
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: getting wrong figures from script

Post 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.
DelPazzo
Forum Newbie
Posts: 23
Joined: Thu Dec 30, 2010 3:49 am

Re: getting wrong figures from script

Post 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
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: getting wrong figures from script

Post 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.
DelPazzo
Forum Newbie
Posts: 23
Joined: Thu Dec 30, 2010 3:49 am

Re: getting wrong figures from script

Post 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
Last edited by DelPazzo on Sun Jan 09, 2011 4:49 am, edited 1 time in total.
adamdressller81
Forum Newbie
Posts: 1
Joined: Tue Jan 04, 2011 5:51 pm

Re: getting wrong figures from script

Post by adamdressller81 »

thank you califdon for yout help and support.
DelPazzo
Forum Newbie
Posts: 23
Joined: Thu Dec 30, 2010 3:49 am

Re: getting wrong figures from script

Post by DelPazzo »

adamdressller81 wrote:thank you califdon for yout help and support.
???
DelPazzo
Forum Newbie
Posts: 23
Joined: Thu Dec 30, 2010 3:49 am

Re: getting wrong figures from script

Post 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.
Post Reply