Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: rounding to nearest

by mojotoad (Monsignor)
on Mar 22, 2003 at 15:40 UTC ( [id://245154]=note: print w/replies, xml ) Need Help??


in reply to rounding to nearest

use Math::Round qw(nearest); $quote = 418.00; $price = nearest(10, $quote);

I don't know if it's important, but this implementation deals properly with negative numbers. From Math::Round:

sub nearest { my ($targ, @inputs) = @_; my @res = (); my $x; $targ = abs($targ) if $targ < 0; foreach $x (@inputs) { if ($x >= 0) { push @res, $targ * int(($x + $half * $targ) / $targ); } else { push @res, $targ * POSIX::ceil(($x - $half * $targ) / $targ); } } return (wantarray) ? @res : $res[0]; }

Cheers,
Matt

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://245154]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-19 16:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found