format problem with numbers above 1000 !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

format problem with numbers above 1000 !RESOLVED

Post by DelPazzo »

Hi,

I have a problem with some script taking figures out of money fields from a mssql db.
it works fine as long as the number is below 1000. but as soon as it is higher than 1000 the system sets the number to 1 when adding it to another number...

e.g. 1038,40 -> 1 and not 1,03 or something like that but just 1

in one loop I would have
1026,45 + 29,50
when echoing these separately they show up as they should. when adding them, the result is 30,50 and it doesn't matter at what point in the script I add them...

Code: Select all

         $recordSet5 = &$conn->Execute('select curren from waehrungen where id= '.$curren);
         $currenc = $recordSet5->fields['curren'];

         $recordSet3 = &$conn->Execute('select auftrag_id, text, preis from translat where rechn = 1 and druck = 0 and kunde = '.$_POST["kunde"]);
         $total = 0;
         $total2 = 0;
         $mwsttot = 0;
         $mwsttot2 = 0;
         $counter = 0;
         while (!$recordSet3->EOF) {
                $auftrag_id = $recordSet3->fields['auftrag_id'];
                $text = $recordSet3->fields['text'];
                $preis = $recordSet3->fields['preis'];
                $preis = $preis;
                $preis = number_format($preis,2);
                $total = $total+$preis;
                echo $total;
                exit;
                $total = number_format($total,2);
                $preis = str_replace(",",";",$preis);
                $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();
         }
Last edited by DelPazzo on Tue Feb 01, 2011 3:07 am, edited 1 time in total.
DelPazzo
Forum Newbie
Posts: 23
Joined: Thu Dec 30, 2010 3:49 am

Re: problem with adding figures above 1000

Post by DelPazzo »

Ok,

Found the solution. it seems that lots of people on the net have this problem...

here is the solution:

In a numeric context, PHP will automatically convert any string to a numeric value. Strings will be converted into two types of numeric values, double floating number and integer, based on the following rules:

* The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).
* If the valid numeric data contains '.', 'e', or 'E', it will be converted to a double floating number. Otherwise, it will be converted to an integer.


this means that if you are working with numbers you'll have to replace every ',' or other sign with '.' !!!

IT'S THAT SIMPLE AND OBVIOUS THAT IT ALREADY HURTS BUT JUST BECAUSE OF THIS SIMPLICITY WE DON'T EVEN THINK ABOUT IT!
Post Reply