help with float numbers
| From: | pdmytrew at lexmark dot com | Date: | Fri, 09 May 2003 18:11:17 +0000 |
| Subject: | help with float numbers | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-147056@lists.php.net to get a copy of this message | ||
Learned members of the list:
I have quite a frustrating problem that I *think* involves floating-point
('double') numbers. I have the following code:
$Total = array_sum($Activity);
if ($Total == 100) {
// do some stuff
} else {
$Message = "Total is $Total! Please check your numbers.\n";
}
If $Activity has values like (25 , 25 , 25 , 25), then it works just fine.
But if $Activity is an array of the following: (71.8 , 5.6 , 5.6 , 8.5 , 8.5)
I get the message "Total is 100! Please check your numbers."
I don't understand!!!
If Total is 100, then why doesn't that meet the condition of the first if-statement ?!
I suspect it has something to do with the way PHP deals with integers vs. floating-point numbers.
Yes?
For my purposes, $Activity may have all integers, all decimals, or a combination of both like (25,
25, 25, 12.5, 12.5).
I'm using PHP 4.3.1. What can I do to allow for this? How do I compare an integer (100) to what
MAY OR MAY NOT be a float ($Total)?
Many thanks, Paul D