|
Posted by Rik Wasmus on 02/01/08 17:25
On Fri, 01 Feb 2008 17:40:21 +0100, The Natural Philosopher <a@b.c> wrot=
e:
> The Natural Philosopher wrote:
>> Rik Wasmus wrote:
>>> On Fri, 01 Feb 2008 16:59:18 +0100, The Natural Philosopher <a@b.c> =
=
>>> wrote:
>>>> The question says it all.
>>>>
>>>> I have an input box, which I fill in with a price.
>>>> IOt gets passed to the main form as a variable, then shoved into an=
=
>>>> SQL field vue a print '%d' statement where the argument is =
>>>> $price*100.
>>>
>>> Could you give us the exact code & input values? And what database =
>>> (&version) are you using?
>>>
>>>> For some reaosn, this particular value goes to 6998.
>>>>
>>>> If I update hee database manually to 6999, it displays as 69.99.
>>>>
>>>> If I enter 69.999 it updates as 6999..not 69999
>>>
>>> Seems to have something to do with float/double precision. However, =
=
>>> the precision isn't that big, and I cannot reproduce it in PHP 5.2 =
>>> here..
>>>
>>> PHP5.2.4:
>>> <?php echo 69.99*100 ?> =3D> output 6999
>>> MySQL5.0.45-community-nt:
>>> SELECT CAST(69.99*100 AS UNSIGNED); =3D> 6999
>> Rik. Its worse than that NOTHING to do with databases.
>> Here is a code fragment.
>> echo $sale_price."<br>\r\n"; // gives 69.99
>> printf ("%d<br>\r\n",($sale_price*100)); //gives 6998
>> $xxx=3D$sale_price*100;
>> echo $xxx."<br>\r\n"; // gives 6999
>> printf ("%d<br>\r\n",$xxx); //gives 6998
>> Now $sale_price comes from a text type input via a _POST_ variable,=
=
>> so I guess its 'text' as its raw form PHP invisible casting drives me=
=
>> nuts, so you can tell ME what $xxx is...
>
>
> Oh my god.I AM GOING MAD..
>
> I put in this one
> printf ("%s<br>\r\n",$xxx);
>
> AND THAT GIVES 6999 !!!!
>
> WTF is php playing at?
>
> I tried printf("%d" ,6999) and printf ("%d",'6999') and those work
>
> But printf("%d",69.99*100) does NOT!
<?php
echo intval(69.99*100);
?>
...will also give 6998.
<?php
$price =3D intval(strval(69.99*100));
printf ("%d",$price);
?>
...will give 6999
'casting' to integer is something entirily different then rounding. If y=
ou =
need the one, don't use the other.
<?php printf("%.0f",69.99*100);?>
...correctly gives 6999 as return.
Or:
<?php printf("%d",round(69.99*100,0)); ?>
-- =
Rik Wasmus
[Back to original message]
|