in reply to strange rounding

Oh no, not that FAQ again.

Anyway, the best way to round to two decimals, is using sprintf:

$units = sprintf "%.2f", $units*$p;
In case you don't like trailing zeroes (maybe you do), as in "124.00", you can get rid of them using
$units =~ s/\.?0+$//;
turning "123.50" into "123.5", and "124.00" into "124".

Only do this when you know your number contains a decimal point.

Replies are listed 'Best First'.
Re^2: strange rounding
by osunderdog (Deacon) on Nov 29, 2004 at 15:24 UTC

    I have found Math::Round a great package for rounding.

    $perl -MMath::Round -e "print nearest(0.01, 3.14159)" 3.14

    "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.


    OSUnderdog