http://www.perlmonks.org?node_id=200049


in reply to Rounding

$rounded=sprintf ("%.1f",$r+.05);
This solves your problem :-)

Replies are listed 'Best First'.
Re^2: Rounding
by Aristotle (Chancellor) on Sep 23, 2002 at 09:26 UTC
    That causes 1.3 to be rounded to 2.0. I don't believe that's what the poster wanted.

    Makeshifts last the longest.

      No, it does not.
      I ran
      $r = 1.3; print $r,"\n"; $rounded=sprintf ("%.1f",$r+.05); print $rounded,"\n";
      and got
      perl -w round.pl 1.3 1.4
      Did you miss a 0 after the decimal? or is there some other behaviour I a missing.
        If you look a bit further,  1.4 + 0.05 prints as 1.4, not 1.5
        Argh. Sorry, was in a hurry and read to carelessly and then didn't notice the extra 0 even after tripplechecking - I didn't think anyone could be that stupid. Turns out my instinct was right, and you were too. Sorry again.

        Makeshifts last the longest.