in reply to Re: rounding to nearest integer
in thread rounding to nearest integer

But that breaks on negative numbers. Best to stick with the FAQ answers rather than making up your own. {sigh}

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: Re: rounding to nearest integer
by allolex (Curate) on May 11, 2003 at 19:42 UTC

    I supposed you could get the int() method to work on negatives by using abs() and some arithmetic with it.

    if ( $value < 0 ) { $rounded = ( int( abs($value) + 0.5) ); $rounded = $rounded - ( $rounded * 2 ); } else { $rounded = int($value + 0.5) }

    or something...

    Seems more complicated than sprintf() =)

    --
    Allolex

    Update: Added closing bracket. (2003-05-15 19:27:26 CEST)