Re: Round
| From: | Jim Lucas | Date: | Wed, 29 Aug 2007 17:42:34 +0000 |
| Subject: | Re: Round | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-261468@lists.php.net to get a copy of this message | ||
Daniel Brown wrote:
On 8/29/07, Zoltán Németh <znemeth@alterationx.hu> wrote:Exactly my point, abs() is not the answer if he had any negative number that did not round to zero, say it would round to -2, then having the abs() in the calculations would return 2 instead of -2, which would be wrong. From what I read from the OP, I don't think this is what he was looking for. the op was asking why he got -0 instead of 0. not for a solution to fix it. ok, better example. <plaintext><?php $list[] = 1; # I expect to get 1 and I get 1 $list[] = -1; # I expect to get -1 but I get 1 $list[] = -1.2; # I expect to get -1 but I get 1 $list[] = -0.23; # I expect to get 0 and I get 0 $list[] = -0.75; # I expect to get -1 and I get 1 foreach ($list AS $value) { var_dump( abs( round($value) ) ); } ?> But, from what the OP says, he would get -0 instead of 0 for the 4th entry. Am I correct with this? if so, you could try casting it as an int like so var_dump( (int)round(-0.26) ); That might fix the problem -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William ShakespeareThink this through before you respond... Try this <?php var_dump( round(-0.26) ); var_dump( abs( round(-0.26) ) ); var_dump( round(-1.26) ); var_dump( abs( round(-1.26) ) ); ?> does this give you the desired results? What if I expected -1 for the last answer?It didn't take much thinking this time.... if you were expecting-1 for the last answer, you'd be wrong. ;-PThe very nature of abs() is to return an absolute number, which isnever a negative.