Rounding up anything past the decimal point sprintf

B

Blnukem

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);

print $answer;



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

Thanx in advance
Blnukem
 
G

Gunnar Hjalmarsson

Blnukem said:
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;
 
V

Vlad Tepes

Blnukem said:
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);
print $answer;

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

Hope this helps,
 
K

ko

Blnukem said:
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);

print $answer;



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

Thanx in advance
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
 
G

Gunnar Hjalmarsson

ko said:
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.

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

ko

Gunnar said:
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.
 
G

Gunnar Hjalmarsson

ko said:
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. :)
 
S

Steve D

Vlad Tepes said:
if ( $item % $count ) { $result = 1 + int $result; }

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
 
K

ko

Gunnar Hjalmarsson said:
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;

print join("\n", $answer, $answer1, $answer2);

====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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,010
Latest member
MerrillEic

Latest Threads

Top