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