Coplan has asked for the wisdom of the Perl Monks concerning the following question:

I'm creating an index of links. Basically, i'm storing and retrieving them from a database. I can find how many links I have to list, and then I want to display only so many at a time (say 20). The thing is, I need to figure out how many pages I will have for the index. So, I divide the total number of links by what I want to display ($total / 20). The obvious problem is that I don't just want to round a number, I want to round up if anything after the decimal exists at all. That way, if I have 21 links, the index will provide two pages for me (21 / 20 = 1.05 -- but I still want to show that last page). I could just assume and add 1 to it, but I only want to do that if there is anything after the decimal place. IE: if I need to display 11.1 pages, I will display 12. If I need to display 11.00 pages, I will only display 11 pages.

This is my current function, minus the unnecessary parts of the code (assume everything is already defined);

my $total = $sth->rows; my $pageCount = sprintf( "%.0f", ($total / 20) );
Any help would be appreciated.
  • Comment on How do I find if anything was removed from using the int or sprintf function?
  • Download Code

Replies are listed 'Best First'.
Re: How do I find if anything was removed from using the int or sprintf function?
by chipmunk (Parson) on Nov 26, 2001 at 06:33 UTC
    Here is one way to do it, using int:
    my $total = $sth->rows; my $perPage = 20; my $pageCount = int(($total + $perPage - 1) / $perPage);
Re: How do I find if anything was removed from using the int or sprintf function?
by IlyaM (Parson) on Nov 26, 2001 at 06:37 UTC
    Another solution using POSIX module:

    use POSIX qw(ceil); my $total = $sth->rows; my $pageCount = ceil($total / 20);
Re: How do I find if anything was removed from using the int or sprintf function?
by belg4mit (Prior) on Nov 26, 2001 at 07:51 UTC
    $pages = ($total- ($remainder = $total % $perpage) )/$perpage; $pages++ if $remainder;