# why does 69.99*100=6998?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

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?

The Natural Philosopher wrote:

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?

The Natural Philosopher wrote:

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?

When dealing with financial data is usually good to either truncate or
round the values before doing much with them.  Those hidden decimal
places will get you  - though sometimes you have to work three digits
out (USPS mailing calculations go deeper).

Family data and accurate money are always two challenges for a
programmer.

## Re: why does 69.99*100=6998?

larry@portcommodore.com wrote:

Most banks and other financial institutions go at least 4 decimal places
PAST the cents (6 decimal places) at a minimum.

They keep the data to these decimal places, but I think if they need to
pay you (i.e. interest), they truncate the value.  But if you need to
pay them, they round the data (or worse yet, ceil it) :-)

The final total for you doesn't change (at least noticeably).  But it
does make a difference to them.

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

## Re: why does 69.99*100=6998?

e:

Could you give us the exact code & input values? And what database  =

(&version) are you using?

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 Wasmus

## Re: why does 69.99*100=6998?

Rik Wasmus wrote:

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

## Re: why does 69.99*100=6998?

The Natural Philosopher wrote:

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?

Jerry Stuckle wrote:

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?

The Natural Philosopher wrote:

round().

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

## Re: why does 69.99*100=6998?

Jerry Stuckle wrote:

A bit verbose, don't you think, Jerry ? <g>

## Re: why does 69.99*100=6998?

bill wrote:

Yeah, but it works.

Jeesh.
I've gotta lotta code to patch.

## Re: why does 69.99*100=6998?

The Natural Philosopher wrote:

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!

## Re: why does 69.99*100=6998?

*** The Natural Philosopher escribió/wrote (Fri, 01 Feb 2008 16:40:21
+0000):

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

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?

e:

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

<?php
\$price = 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

## Re: why does 69.99*100=6998?

Of course they do, because 6999 is an integer.  It is represented exactly
in binary.

Is it simply that you have chosen not to read ANY of the replies that you
are getting here?  This has been carefully explained to you several times,
but you keep expressing surprise.

The number 69.99 cannot be represented exactly in binary.  The closest
binary number that can be stored exactly is just slightly under 69.99.
Thus, when you multiply it by 100, you get 6998.9999...  When printf has to
print it with %d, it needs to convert that to an integer.  It does that by
truncating, so the result is the integer 6998.

This is a natural outcome of working with floating point numbers.  If you
want a result to be rounded, uses round().
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

## Re: why does 69.99*100=6998?

Tim Roberts wrote:

No. its more likley that your ISP got the posts out of order. Look at th
edates dufus.

This has been carefully explained to you several times,

Never miss a chance to score points do you?

Its a natural result of a language that is totally unclear as to what
format a variable is in at any given moment. It all goes back to BASIC
really..

## Re: why does 69.99*100=6998?

The Natural Philosopher wrote:

YOU should read the dates, dufus.  There were several responses before
you again expressed your "surprise".

Never miss a chance to show what an idiot you are, do you?

And you call yourself a programmer?  ROFLMAO.

New programmers may not understand floating point representations, but
they learn quickly.  And it goes back a lot farther than BASIC.

In fact, that's exactly why the DECIMAL data type used in databases, as
well as COBOL and other business languages was created.

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

## Re: why does 69.99*100=6998?

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?

.oO(The Natural Philosopher)

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

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

Micha