Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: How do I print/round a number to a given number of decimal places?

by Munkey (Novice)
on Aug 26, 2015 at 22:34 UTC ( [id://1140129]=note: print w/replies, xml ) Need Help??


in reply to How do I print/round a number to a given number of decimal places?

Perhaps not as fast as printf, but pretty fast, and not using anything special:
sub round { my ($nr,$decimals) = @_; return (-1)*(int(abs($nr)*(10**$decimals) +.5 ) / (10**$decimals)) + if $nr<0; return int( $nr*(10**$decimals) +.5 ) / (10**$decimals); }
strange that no-one has posted this, seems like the most straightforward idea to me...
  • Comment on Re: How do I print/round a number to a given number of decimal places?
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1140129]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-25 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found