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

Is there a way that I can round up if ANYTHING exists after the decimal point? I want to be able to set any variable with a value of 11.1 to 12, but I don't want to set 11.0 to 12. If I knew there was always going to be something after the decimal place, I would just use "$num=(int($number)+1;". Obviously, that won't work if the number is already a whole number.

Basically...is there a way to set the sensativity of sprintf? Is there a way that I can plainly say round up if anything exists after the decimal?

Here's what I have so far:

my $total = $sth->rows; my $pageCount = sprintf( "%.0f", ($total / 20) );
--Coplan

Replies are listed 'Best First'.
Re: How can I round up if any decimal place exists?
by japhy (Canon) on Nov 26, 2001 at 06:37 UTC
    use POSIX 'ceil'; print ceil(11), "\n"; print ceil(11.1), "\n";

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: How can I round up if any decimal place exists?
by jlongino (Parson) on Nov 26, 2001 at 06:42 UTC
    You could use this:
    $j = int($j)+1 if int($j) != $j;

    --Jim

      This (or some variation of it) is working for me just fine. Kudos out to the simple methods.

      Of course, now I'm kicking myself for not thinking of it. Thanks again!

      --Coplan

Re: How can I round up if any decimal place exists?
by cLive ;-) (Prior) on Nov 26, 2001 at 06:55 UTC
    Know how many DP you're dealing with? If /20, then 2 in .05 increments, so:
    my $pageCount = int( ($total/20) + .99 );
    will work fine.

    timtowtdi :-)

    cLive ;-)

Re: How can I round up if any decimal place exists?
by Dr. Mu (Hermit) on Nov 26, 2001 at 09:56 UTC
    I've often used the following for counting pages, when $total is an integer number of rows:
    use constant ROWS_PER_PAGE => 20; my $pageCount = int(($total + ROWS_PER_PAGE - 1) / ROWS_PER_PAGE);
    Using the constant, BTW, lets you change your mind easily if your pages grow or shrink.