Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Rounding off numbers

by Tanalis (Curate)
on Apr 20, 2006 at 14:22 UTC ( #544607=note: print w/replies, xml ) Need Help??


in reply to Rounding off numbers

I always liked
$int = sprintf "%.0f", $float;
which rounds things off nicely.

Replies are listed 'Best First'.
Re^2: Rounding off numbers
by bulrush (Scribe) on Jan 25, 2016 at 14:22 UTC
    <s>sprintf and printf no longer rounds sometime after Perl 5.8.8. It truncates. </s>

    EDIT: I think I may have some bad information, which was corrected later on. If a calculation is done which results in 2.4999999999, sprintf("%.2f",2.49999999) would "round" to 2.49 for some reason, which appears to be truncation, but is actually a different problem. You may not be able to reproduce the problem by typing in 2.4999999. 2.49999999 has to be the result of a calculation.

      It truncates.
      Does it?
      $ perl -wE 'printf "%.3f\n", .0009 + $];' 5.023
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://544607]
help
Chatterbox?
[stevieb]: ahhh yes, it *is* a ref. I had forgotten that. I too have learned something. I quit too!!
[marioroy]: Is List::Util::XS a core Perl module?
[stevieb]: corelist List::Util::XS says "List::Util::XS was first released with perl v5.10.1"
[stevieb]: so yes, since v5.10.1 :)
[MidLifeXis]: According to [metaperl:// corelist], Perl 5.10.1
Lady_Aleena blurbles. One of her favorite modiles is now in "maintenance mode" in favor of a new fangled module.
[MidLifeXis]: D'oh. Slow out of the gate.
[marioroy]: Thanks stevieb, MidLifeXis.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2017-05-24 17:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?