Re: Round

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Daniel Brown wrote:
On 8/29/07, Zoltán Németh <znemeth@xxxxxxxxxxxxxx> wrote:
Think 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.  ;-P

    The very nature of abs() is to return an absolute number, which is
never a negative.


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 Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux