Reply to Re: why does 69.99*100=6998?

Your name:

Reply:


Posted by Jerry Stuckle on 02/01/08 17:04

The Natural Philosopher wrote:
> Jerry Stuckle wrote:
>> 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 ?> => output 6999
>>>> MySQL5.0.45-community-nt:
>>>> SELECT CAST(69.99*100 AS UNSIGNED); => 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=$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...
>>>
>>
>> According to the PHP manual:
>>
>> %d - the argument is treated as an integer, and presented as a
>> (signed) decimal number.
>>
>> Which means it is truncating instead of rounding.
>>
>> 69.99 is not exact. It's approximately 69.98999999999999488409.
>>
>> So 69.99*100 comes out to be 6998.999999999999488409 and truncated you
>> get 6998.
>>
> Right. So every other php funtion treats 6999 as 6999,. except printf's %d.
>
> If you use %s, it works..
>
> Invisible casting has to be the worst thing ever. That's WHY I did it
> all in cents (or pence)to avoid these pissing float issues.
>
> Is there a function that takes 69.99*100 and makes it a properly rounded
> integer?
>
>
>
>
>
>

round().

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация