why does 69.99*100=6998?

why does 69.99*100=6998?

am 01.02.2008 16:59:18 von Courtney

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.

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

I must be doing something really dumb here..

Re: why does 69.99*100=6998?

am 01.02.2008 17:12:00 von Jerry Stuckle

The Natural Philosopher 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.
>
> 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
>
> I must be doing something really dumb here..
>

Because floating point numbers are almost always not exact. The .99
cannot be represented exactly in floating point - it's a repeating
number, like 1/3 is in decimal.

I store prices in cents (i.e. 6999) then display them as dollars.

Alternatively, you could use the DECIMAL datatype in your database.

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

Re: why does 69.99*100=6998?

am 01.02.2008 17:12:56 von Courtney

The Natural Philosopher 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 $sale_price*100.
>
> 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
>
> I must be doing something really dumb here..

Its even weirder.

I put in some checks:

echo $sale_price gives me 69.99
Echo ($sale_price*100) gives me 6999

But printf ("%d",$sale_price*100);

gives me 6998!!!

WTF is going on?

Re: why does 69.99*100=6998?

am 01.02.2008 17:15:05 von luiheidsgoeroe

On Fri, 01 Feb 2008 16:59:18 +0100, The Natural Philosopher wrot=
e:
> 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 SQ=
L =

> 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:
=3D> output 6999
MySQL5.0.45-community-nt:
SELECT CAST(69.99*100 AS UNSIGNED); =3D> 6999
-- =

Rik Wasmus

Re: why does 69.99*100=6998?

am 01.02.2008 17:29:28 von Courtney

Rik Wasmus wrote:
> On Fri, 01 Feb 2008 16:59:18 +0100, The Natural Philosopher 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:
> => 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."
\r\n"; // gives 69.99
printf ("%d
\r\n",($sale_price*100)); //gives 6998
$xxx=$sale_price*100;
echo $xxx."
\r\n"; // gives 6999
printf ("%d
\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...

Re: why does 69.99*100=6998?

am 01.02.2008 17:37:49 von Jerry Stuckle

The Natural Philosopher wrote:
> Rik Wasmus wrote:
>> On Fri, 01 Feb 2008 16:59:18 +0100, The Natural Philosopher
>> 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:
>> => 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."
\r\n"; // gives 69.99
> printf ("%d
\r\n",($sale_price*100)); //gives 6998
> $xxx=$sale_price*100;
> echo $xxx."
\r\n"; // gives 6999
> printf ("%d
\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.

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

Re: why does 69.99*100=6998?

am 01.02.2008 17:40:21 von Courtney

The Natural Philosopher wrote:
> Rik Wasmus wrote:
>> On Fri, 01 Feb 2008 16:59:18 +0100, The Natural Philosopher
>> 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:
>> => 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."
\r\n"; // gives 69.99
> printf ("%d
\r\n",($sale_price*100)); //gives 6998
> $xxx=$sale_price*100;
> echo $xxx."
\r\n"; // gives 6999
> printf ("%d
\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
\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!

Re: why does 69.99*100=6998?

am 01.02.2008 17:49:57 von Luuk

"The Natural Philosopher" schreef in bericht
news:1201883369.32447.0@proxy00.news.clara.net...
> Rik Wasmus wrote:
>> On Fri, 01 Feb 2008 16:59:18 +0100, The Natural Philosopher
>> 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:
>> => 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."
\r\n"; // gives 69.99
> printf ("%d
\r\n",($sale_price*100)); //gives 6998
> $xxx=$sale_price*100;
> echo $xxx."
\r\n"; // gives 6999
> printf ("%d
\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...

the value of $xxx is not close enough to 6999,
so, printf("%d", $xxx) will truncate this

like printf("%d", 2/3) gives the obvious result of '0'

try printf("%e", $xxx-6999)
and you'll get -9.09e-13

Re: why does 69.99*100=6998?

am 01.02.2008 17:53:14 von Courtney

Jerry Stuckle wrote:
> The Natural Philosopher wrote:
>> Rik Wasmus wrote:
>>> On Fri, 01 Feb 2008 16:59:18 +0100, The Natural Philosopher
>>> 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:
>>> => 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."
\r\n"; // gives 69.99
>> printf ("%d
\r\n",($sale_price*100)); //gives 6998
>> $xxx=$sale_price*100;
>> echo $xxx."
\r\n"; // gives 6999
>> printf ("%d
\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?

Re: why does 69.99*100=6998?

am 01.02.2008 18:04:50 von Jerry Stuckle

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
>>>> 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:
>>>> => 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."
\r\n"; // gives 69.99
>>> printf ("%d
\r\n",($sale_price*100)); //gives 6998
>>> $xxx=$sale_price*100;
>>> echo $xxx."
\r\n"; // gives 6999
>>> printf ("%d
\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
==================

Re: why does 69.99*100=6998?

am 01.02.2008 18:06:46 von webmasterNOSPAMTHANKS

*** The Natural Philosopher escribió/wrote (Fri, 01 Feb 2008 16:40:21
+0000):
> I put in this one
> printf ("%s
\r\n",$xxx);
>
> AND THAT GIVES 6999 !!!!
>
> WTF is php playing at?

PHP does exactly the same as almost any other programming language: it
handles numbers in binary format because that's how computers work. It's
basic computer science. Just follow this guidelines, that are not exclusive
to PHP:

- In your DB, store prices as DECIMAL.
- In your PHP code, always use round() or number_format() to display
numbers. Avoid truncation.
- Do not lose precision when not necessary. E.g.: rather than rounding and
adding, add first and round later.

If precision is soooo important that you can't afford missing a single
cent, do not use regular operators (+, -, * and /). Instead of that, use an
arbitrary precision library that won't convert numbers into binary:

http://www.php.net/bc




--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor austrohúngaro: http://www.demogracia.com
--

Re: why does 69.99*100=6998?

am 01.02.2008 18:25:43 von luiheidsgoeroe

On Fri, 01 Feb 2008 17:40:21 +0100, The Natural Philosopher wrot=
e:

> The Natural Philosopher wrote:
>> Rik Wasmus wrote:
>>> On Fri, 01 Feb 2008 16:59:18 +0100, The Natural Philosopher =
=

>>> 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:
>>> =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."
\r\n"; // gives 69.99
>> printf ("%d
\r\n",($sale_price*100)); //gives 6998
>> $xxx=3D$sale_price*100;
>> echo $xxx."
\r\n"; // gives 6999
>> printf ("%d
\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
\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!

echo intval(69.99*100);
?>
...will also give 6998.

$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.

...correctly gives 6999 as return.
Or:

-- =

Rik Wasmus

Re: why does 69.99*100=6998?

am 01.02.2008 20:23:05 von NerdRevenge

Jerry Stuckle wrote:
> 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
>>>>> 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:
>>>>> => 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."
\r\n"; // gives 69.99
>>>> printf ("%d
\r\n",($sale_price*100)); //gives 6998
>>>> $xxx=$sale_price*100;
>>>> echo $xxx."
\r\n"; // gives 6999
>>>> printf ("%d
\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().
>
A bit verbose, don't you think, Jerry ?

Re: why does 69.99*100=6998?

am 01.02.2008 21:11:55 von Michael Fesser

..oO(The Natural Philosopher)

>Right. So every other php funtion treats 6999 as 6999,. except printf's %d.

Of course a 6999 is treated as a 6999. But your value wasn't a 6999 int,
it was a float.

>If you use %s, it works..
>
>Invisible casting has to be the worst thing ever.

I would say in this case it was a user error.

Micha