Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I realise this is more than an algorithmic question, but figured someone here has a higher level of math geekyness than i.

What i want to do is round an integer to the closest unit.

In this specific case, i'm running a cronjob every five mins, and putting the data into a database for later processing.

What i'm after is rounding the timestamp from the database to the nearest 5 mins. I've come up with something, which seems to work, however i'm wondering if this is the most efficient way to do it.

#!/usr/bin/perl -w use strict; my @time = localtime(time); my $prec = 5; my $number = 52; $\="\n"; my $div = $number%$prec; $div = 1 if ($div == 0); my $num = sprintf("%.1f", $number/$div ); my $intnum = int sprintf("%.1f", $number/$div ); if ( ($num-$intnum) >= .5) { print $prec-($number%$prec)+$number; } else { print $number-($number%$prec); }

In reply to OT: Integer rounding? by Ryszard

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-16 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found