in reply to Rounding function

I pulled this one out of my bag of holding today...
sub round {
    my ($number,$decimals) = @_;
    return substr($number+("0." . "0" x $decimals . "5"),
                  0, $decimals+length(int($number))+1);
}
brother Willem

--
wrvhage@science.uva.nl.nl | http://www.xs4all.nl/~wrvh

Replies are listed 'Best First'.
Re^2: Rounding function
by mythos (Sexton) on Oct 30, 2007 at 23:04 UTC
    I'm a complete amateur trying to teach myself to program. After two days of scouring the Camel and Llama books (nobody tell merlyn), your subroutine was exactly what I wanted. I should have checked the monks first. Thank you for this.
      It is amazing to me that this type of function is not built in. I just wanted to thank you for sharing. This simple yet obvious solution is great. Thanks again and God bless, -David