Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Perl rounder...

by abaxaba (Hermit)
on Apr 08, 2002 at 21:05 UTC ( #157555=snippet: print w/replies, xml ) Need Help??
Description: Just a little hack to round numbers. Strips leading zeros, pads the precision of you want more places then your decimal length...


sub main
    my ($num,$prec)=@ARGV;
    my ($mant,$dec) = split (/\./,$num);
    my $round = join ("",($mant,length($dec)?".":"",round($dec)));
    output ($round);
sub round
    my $dec=shift;

sub output
    print "Round = ",shift,"\n";

Replies are listed 'Best First'.
Re: Perl rounder...
by maverick (Curate) on Apr 08, 2002 at 22:21 UTC
    Sorry dude, it's already been done.
    printf takes a sting that describes what you want to print. In your case a floating point number (f) with a precision that you specify as the second arg ($ARGV[1]) using the value in $ARGV[0]

    OmG! They killed tilly! You *bleep*!!

      Explains the negative comments votes.... REAP it then.
Re: Perl rounder...
by gav^ (Curate) on Apr 08, 2002 at 22:36 UTC
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://157555]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2016-10-23 23:27 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (302 votes). Check out past polls.