There is nothing wrong with your approach at all - Another method might be to employ the sprintf operator for which there is an excellent tutorial on this site. eg.

sub roundThatBadDog { my $seconds = shift; return sprintf("%d", $seconds); }

Or alternatively, if you are wanting precision, replace the %d with %.xf where x is a integer value representing the order of decimal precision desired.

 

Update

 

In response to japhy's comments, I checked out the FAQ and ran a few timing tests to compare.

From my reading, the comments in the FAQ hardly suggest the avoidance of sprintf function for rounding purposes but rather the use of a more intrinsic, known method of rounding (such as those submitted by japhy here) for sensitive or high-value operations. The example given is a financial environment where the FAQ rightly suggests that a programmer should implement their own rounding code rather than relying on system functions.

With regard to a timing comparison, the following is the output - Make of it what you will :-)

Benchmark: timing 100000 iterations of japhy, rob_au... japhy: 3 wallclock secs ( 4.13 usr + 0.00 sys = 4.13 CPU) @ 24 +213.08/s (n=100000) rob_au: 4 wallclock secs ( 4.23 usr + 0.00 sys = 4.23 CPU) @ 23 +640.66/s (n=100000)

 

Update

 

These results may not be truly indicative of function performance, and may I add, not intendedly so. japhy discusses performance differences in his more comprehensive comparison here. ++japhy!

 

And the (now superceded) code used for the benchmark timings ...

#!/usr/bin/perl use Benchmark; use strict; timethese(100000, { 'rob_au' => q! my @values = ( 1.24, 5.43, -98.54, -73.667, 0.67, 2.34, 76.89, + -999.99, 34.52, 67.89 ); foreach my $value (@values) { my $result = sprintf("%d", $value); } !, 'japhy' => q! my @values = ( 1.24, 5.43, -98.54, -73.667, 0.67, 2.34, 76.89, + -999.99, 34.52, 67.89 ); foreach my $value (@values) { my $result = int($value * 10**0 + .5 * ($value < 0 ? -1 : +1)) / 10**0; } ! });
perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

In reply to Re: Preferred Methods by rob_au
in thread Preferred Methods by vek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.