in reply to Re: Help with decimals
in thread Help with decimals

Your plucking the digits off one end, sticking on the dot, and sticking them back on sounds like something a regex should do!

$number .= '0' while length($number) < $places; # add trailing zeros if needed $number =~ /(.{$places})/\.$1/;
Or a substr
# add trailing zeros, as above. substr ($number, -$places, 0, '.');

Replies are listed 'Best First'.
Re: Re: Re: Help with decimals
by rchiav (Deacon) on Jul 31, 2001 at 01:45 UTC
    Acutally a regex isn't all that good of an idea. It's very slow. But substr is better than chop.. benchmarks included..

    Benchmark: timing 100000 iterations of chop, math, regex, substr... chop: 6 wallclock secs ( 4.92 usr + 0.03 sys = 4.95 CPU) @ 202 +02.02/s (n=100000) math: 2 wallclock secs ( 1.07 usr + 0.02 sys = 1.09 CPU) @ 917 +43.12/s (n=100000) regex: 25 wallclock secs (17.40 usr + 0.32 sys = 17.72 CPU) @ 56 +43.34/s (n=100000) substr: 2 wallclock secs ( 1.99 usr + 0.03 sys = 2.02 CPU) @ 495 +04.95/s (n=100000)
    Doing the math is still the best option..