Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Simple Rounding

by einerwitzen (Sexton)
on Mar 25, 2004 at 15:35 UTC ( #339773=perlquestion: print w/ replies, xml ) Need Help??
einerwitzen has asked for the wisdom of the Perl Monks concerning the following question:

I have a number, 1100.64686081907

I just need a simple command to round this to 1100.65

Is there a simple way to do this?

Comment on Simple Rounding
Re: Simple Rounding
by borisz (Canon) on Mar 25, 2004 at 15:39 UTC
    use printf or sprintf.
    printf "%.2f", 1100.64686081907;
    Boris
Re: Simple Rounding
by b10m (Vicar) on Mar 25, 2004 at 15:44 UTC

    You probably want to use sprintf:

    $foo = 1100.64686081907; $bar = sprintf("%.2f", $foo);

    Now $bar will contain 1100.65

    Update: changed variable names from $a, $b to $foo, $bar for the first are "special" globals (thanks for the warning, davido)

    --
    b10m

    All code is usually tested, but rarely trusted.
Re: Simple Rounding
by Roy Johnson (Monsignor) on Mar 25, 2004 at 15:52 UTC
    Be sure to browse through the Q&A section, where you'll find answers to this question and other commonly asked ones.

    The PerlMonk tr/// Advocate
Re: Simple Rounding
by Fletch (Chancellor) on Mar 25, 2004 at 15:53 UTC

    Not to mention the always popular FGA perldoc -q round . . .

Re: Simple Rounding
by Beechbone (Pilgrim) on Mar 25, 2004 at 15:54 UTC
    $a = 1100.64686081907; $b = int($a*100+.5)/100;
    but if you just want to print it, (s)printf() would be the better choice.

    Search, Ask, Know

      My first thought was, 'Silly guy, why is he writing C code? Doesn't he know the Perl(tm) way?'.

      Then I thought, sprintf() is expensive .... how fast is it to run the integer math?

      --
      TTTATCGGTCGTTATATAGATGTTTGCA

        There is a subtle, but important difference between
        $var1 = sprintf "%.2f" => $number;
        and
        $var2 = int ($number * 100 + .5) / 100;
        $var1 is a PV, that is, the interval variable has a string value, but not a numeric value, while $var2 is an NV, that is, it has a numeric value, but not a string value. This means that $var1 will have two digits after the decimal point when printed - sprintf has garanteed that. But that's not necessarely true for $var2. Since you cannot represent 1/100 exactly in binary, you left the possibility open that if you stringify the number, you end up with more than 2 characters after the decimal point. Now, it may not happen in a thousand testcases, but can you guarantee it will never happen?

        See perldoc -q decimal.

Re: Simple Rounding
by Not_a_Number (Parson) on Mar 25, 2004 at 18:23 UTC

    As Abigail-II says, sprintf will guarantee two digits after the decimal point. But it won't guarantee consistent rounding. Try this:

    my @nums = ( 62, 63, 64, 65 ); for ( @nums ) { $_ += 0.005; printf "%.2f\n", $_; }

    Output (at least on my machine):

    62.01 63.01 64.00 65.00

    To quote Zaxo from the thread (s)printf and rounding woes:

    "That is a general property of binary floating-point numbers... If you need consistency in a fixed-point decimal representation, you should scale the numbers to be represented as integers."

    dave

Re: Simple Rounding
by PERLscienceman (Curate) on Mar 26, 2004 at 02:50 UTC
    Greetings Fellow Monk!
    In the standing philosophy of "There Is Always More Than One Way To Do It" I found this CPAN module, Math::Round, which should also do what you are looking for. I have included a chunk of example/test code below:
    #!/usr/bin/perl -w use strict; use Math::Round; my $number=1100.64686081907; $number=nearest(.01,$number); print "$number\n"; ___OUTPUT___ 1100.65

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://339773]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-07-26 17:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (178 votes), past polls