in reply to Re: Round down to '.5' or '.0'
in thread Round down to '.5' or '.0'

Conversely, is there another trick to do the same but round up?

Replies are listed 'Best First'.
Re^3: Round down to '.5' or '.0'
by ikegami (Patriarch) on Oct 24, 2007 at 20:10 UTC
    use POSIX qw( ceil ); my $rounded = ceil($num*2)/2;

    Update: Oops, the above didn't work for negative numbers.

    use POSIX qw( ceil ); my $rounded = ($num>=0 ? +1 : -1) * ceil(abs($num)*2)/2;

    or

    use POSIX qw( ceil floor ); my $rounded = ($num >= 0 ? ceil($num*2)/2 : floor($num*2)/2 );