Philip Newton <(E-Mail Removed)> wrote in comp.lang.perl.misc:

> On 27 Oct 2003 11:27:56 GMT, (E-Mail Removed)-berlin.de (Anno

> Siegel) wrote:

>

> > The FAQ answer is "use sprintf()", which is fine in most cases. It

> > must be said, however, that sprintf() is a slow function, and if a

> > lot of rounding is going on it can easily dominate the calculation.

> > Even a pure Perl rounding function, along the lines of

> >

> > sub round {

> > my $x = shift;

> > my $y = 0.5 + abs $x;

> > my $abs = int $y;

> > $abs -= $abs % 2 if $y == $abs;

> > ($x <=> 0) * $abs;

> > }

> >

> > is twice as fast, and a compiled rounding function can be ten times

> > as fast.

>

> But that doesn't allow you to specify the number of decimal places to

> round to, does it?
No, it doesn't. I don't remember the last time I wanted to round to

anything but the nearest integer, but the objection is valid.

> And I wonder whether, once you add the scaling necessary to support

> that, it's still faster than sprintf.
It (i.e. my implementation on my machine) is still 44% faster than sprintf,

as opposed to 113% for the non-scaling version. Then again, it can also

round 1234 to 1000 for a negative "number of decimal places", something

sprintf doesn't do.

But we're approaching bean-counting territory here...

Anno