Rounding up anything past the decimal point sprintf

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

  1. Blnukem

    Blnukem Guest

    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
     
    Blnukem, Sep 4, 2003
    #1
    1. Advertising

  2. 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;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 4, 2003
    #2
    1. Advertising

  3. Blnukem

    Anno Siegel Guest

    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;
    my $answer = ceil( $result);

    Anno
     
    Anno Siegel, Sep 4, 2003
    #3
  4. Blnukem

    Vlad Tepes Guest

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


    > print $answer;
    >
    > This returns a value of 2.20 I would like it to return a value of 3.00
    >


    Hope this helps,
    --
    Vlad
     
    Vlad Tepes, Sep 4, 2003
    #4
  5. Blnukem

    ko Guest

    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);
    >
    > 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
     
    ko, Sep 4, 2003
    #5
  6. 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
    > -268.99999999999994315658 instead.


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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 4, 2003
    #6
  7. Blnukem

    ko Guest

    Gunnar Hjalmarsson 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? 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
    #7
  8. ko wrote:
    > Gunnar Hjalmarsson 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. :)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 4, 2003
    #8
  9. Blnukem

    Steve D Guest

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

    >
    > > print $answer;
    > >
    > > 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
    #9
  10. Blnukem

    ko Guest

    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;

    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.
     
    ko, Sep 4, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chad
    Replies:
    6
    Views:
    321
  2. Saraswati lakki
    Replies:
    0
    Views:
    1,348
    Saraswati lakki
    Jan 6, 2012
  3. Robert TV

    Rounding numbers with sprintf

    Robert TV, Jun 12, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    244
    Gunnar Hjalmarsson
    Jun 12, 2004
  4. PERO
    Replies:
    4
    Views:
    456
    Brian McCauley
    Apr 13, 2007
  5. Replies:
    5
    Views:
    124
    Ilya Zakharevich
    Jan 24, 2008
Loading...

Share This Page