# Rounding up anything past the decimal point sprintf

Discussion in 'Perl Misc' started by Blnukem, Sep 4, 2003.

1. ### BlnukemGuest

Hi all

How can I round a number up if there is any value other than 00 after the
decimal point here is what I have:

my \$count = "5";
my \$item = "11";

my \$result = (\$item / \$count);

my \$answer = sprintf ('%.2f', \$result);

This returns a value of 2.20 I would like it to return a value of 3.00

Blnukem

Blnukem, Sep 4, 2003

Blnukem wrote:
> How can I round a number up if there is any value other than 00
> after the decimal point here is what I have:
>
> my \$count = "5";
> my \$item = "11";
> my \$result = (\$item / \$count);
> my \$answer = sprintf ('%.2f', \$result);
>
> This returns a value of 2.20 I would like it to return a value of
> 3.00

my \$answer = \$result - int \$result ? int \$result + 1 : \$result;

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl

3. ### Anno SiegelGuest

Gunnar Hjalmarsson <> wrote in comp.lang.perl.misc:
> Blnukem wrote:
> > How can I round a number up if there is any value other than 00
> > after the decimal point here is what I have:
> >
> > my \$count = "5";
> > my \$item = "11";
> > my \$result = (\$item / \$count);
> > my \$answer = sprintf ('%.2f', \$result);
> >
> > This returns a value of 2.20 I would like it to return a value of
> > 3.00

>
> my \$answer = \$result - int \$result ? int \$result + 1 : \$result;

use POSIX;

Anno

Anno Siegel, Sep 4, 2003

Blnukem <> wrote:

> How can I round a number up if there is any value other than 00 after the
> decimal point here is what I have:
>
> my \$count = "5";
> my \$item = "11";

Don't quote numbers.

> my \$result = (\$item / \$count);

if ( \$item % \$count ) { \$result = 1 + int \$result; }

> my \$answer = sprintf ('%.2f', \$result);

>
> This returns a value of 2.20 I would like it to return a value of 3.00
>

Hope this helps,
--

5. ### koGuest

Blnukem wrote:
> Hi all
>
> How can I round a number up if there is any value other than 00 after the
> decimal point here is what I have:
>
> my \$count = "5";
> my \$item = "11";
>
> my \$result = (\$item / \$count);
>
> my \$answer = sprintf ('%.2f', \$result);
>
>
>
>
> This returns a value of 2.20 I would like it to return a value of 3.00
>
> Blnukem
>
>

perldoc -f int

int EXPR
int

Returns the integer portion of EXPR. If EXPR is omitted, uses "\$_". You
should not use this function for rounding: one because it truncates
towards "0", and two because machine representations of floating point
numbers can sometimes produce counterintuitive results. For example,
"int(-6.725/0.025)" produces -268 rather than the correct -269; that's
because it's really more like -268.99999999999994315658 instead.
Usually, the "sprintf", "printf", or the "POSIX::floor" and
"POSIX::ceil" functions will serve you better than will int().

And the POSIX docs state that ceil() returns: "the smallest integer
value greater than or equal to the given numerical argument."

====CODE
#!/usr/bin/perl -w
use strict;
use POSIX qw(ceil);

my \$a = 1.234;
\$a = sprintf("%.2f", ceil(\$a) );
print '\$a', " => \$a\n";

====RESULTS
\$a => 2.00

HTH - keith

ko, Sep 4, 2003

ko wrote:
>
> int EXPR
> int
>
> Returns the integer portion of EXPR. If EXPR is omitted, uses "\$_".
> You should not use this function for rounding: one because it
> truncates towards "0", and two because machine representations of
> floating point numbers can sometimes produce counterintuitive
> results. For example, "int(-6.725/0.025)" produces -268 rather than
> the correct -269; that's because it's really more like

But none of those reasons for not using the int() function is
applicable to this case, right?

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl

7. ### koGuest

> But none of those reasons for not using the int() function is
> applicable to this case, right?
>

I don't remember anywhere in my post where I said that it is wrong to
use int()...do you see something I cannot see? Besides, *everyone* knows
that 'There's More Than One Way To Do It'

The quote was merely for the OP's benefit. That's why the reply
referenced the *first* post, not either of the follow-ups that used
examples of int(). Strictly looking at the original snip of code (and
without trying to speak for the OP), it stands to reason that the OP may
not have been sure/aware of how to use int() or POSIX. The quote is a
good example of when to use each of the functions, and the POSIX module
may come in handy in the OP's future.

ko, Sep 4, 2003

ko wrote:
>> But none of those reasons for not using the int() function is
>> applicable to this case, right?

>
> I don't remember anywhere in my post where I said that it is wrong
> to use int()...do you see something I cannot see?

Well, let's say that the context in which you posted the quote left
some room for misunderstandings... I'm glad we are agreed.

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl

9. ### Steve DGuest

Vlad Tepes <> wrote in message news:<bj761g\$hnl\$>...
> Blnukem <> wrote:
>
> > How can I round a number up if there is any value other than 00 after the
> > decimal point here is what I have:

>
> > my \$result = (\$item / \$count);

>
> if ( \$item % \$count ) { \$result = 1 + int \$result; }
> > my \$answer = sprintf ('%.2f', \$result);

>
> >
> > This returns a value of 2.20 I would like it to return a value of 3.00

This function is more properly called the "ceiling" function.

Perl Cookbook p 47 suggests the POSIX "ceil" function:
use POSIX;
my \$result = ceil( \$item/\$count ) ; # \$result == 3

Also, you could just add 0.5 to the value before printing with %f if
you just want to print it.

Of course, using printf with "%f" gives true rounding for printing.

Now if you want true rounding without using printf, use POSIX "floor":
use POSIX;
my \$result = floor( (\$item/\$count) + 0.5 ) ; # \$result == 2

Regards,
Steve D

Steve D, Sep 4, 2003
10. ### koGuest

Gunnar Hjalmarsson <> wrote in message news:<bj7d92\$ga108\$-berlin.de>...

> Well, let's say that the context in which you posted the quote left
> some room for misunderstandings... I'm glad we are agreed.

I still don't see why there would be a misunderstanding, especially
since the OP asked "How can I round a number up if there is any value
other than 00 after the decimal point". In fact, the snippet that you
posted won't round up negative numbers:

====CODE
#!/usr/bin/perl -w
use POSIX qw[ceil];

my \$count = 5;
my \$item = -11;

my \$result = (\$item / \$count);

my \$answer = sprintf ("%.2f", \$result);
my \$answer1 = sprintf ("%.2f", ceil(\$result) );
my \$answer2 = \$result - int \$result ? int \$result + 1 : \$result;

====RESULTS
-2.20
-2.00
-1

That's the reason I posted 'perldoc -f int', only the OP knows whether
or not negative numbers will come in to play.

ko, Sep 4, 2003