You are here: Re: why does 69.99*100=6998? « PHP Programming Language « IT news, forums, messages
Re: why does 69.99*100=6998?

Posted by lvaro G. Vicario on 02/01/08 17:06

*** The Natural Philosopher escribió/wrote (Fri, 01 Feb 2008 16:40:21
+0000):
> I put in this one
> printf ("%s<br>\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
--

 

Navigation:

[Reply to this 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

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