For details read Perl NOC. After June 25

Eric Wilhelm >
Math-Round-Var-v1.0.0 >
Math::Round::Var

Math::Round::Var - Variations on rounding.

Simple decimal rounding:

use Math::Round::Var; my $rnd = Math::Round::Var->new(0.01); # rounds to two decimal places: my $num = 399886.758673; $num = $rnd->round($num); print "$num\n"; # 399886.76

Now it all makes sense.

#!/usr/bin/perl use Math::Round::Var; my $scheme = shift; # let user specify the rounding my $num1 = shift; my $rnd = Math::Round::Var->new($scheme); my $num2 = $rnd->round($num1); print "$num1 rounds to $num2 according to scheme: $scheme\n";

This module gives you the ability to round numbers to either decimal or fractional precision while encapsulating the rounding precision in an object. This allows scripts and modules to maintain multiple precision values as objects.

It also implements flexible scheme parsing, so that your programs and modules can offload the how-to-round decisions to this module.

Eric L. Wilhelm @ <ewilhelm at cpan dot org>

This module is copyright (C) 2003-2008 by Eric L. Wilhelm.

This module is distributed under the same terms as Perl. See the Perl source package for details.

You may use this software under one of the following licenses:

(1) GNU General Public License (found at http://www.gnu.org/copyleft/gpl.html) (2) Artistic License (found at http://www.perl.com/pub/language/misc/Artistic.html)

The Math::Round::Var->new() constructor only decides between the sub-packages based on the format of your precision argument.

This is the extent of the purpose of the Math::Round::Var class.

Math::Round::Var->new($precision);

Returns "decimal" or "fraction" for $type based on the format of $precision. If $type is "decimal", then $count will be the number of digits to use.

my ($type, $count) = format_of($precision);

Valid formats should be any of the number formats which are used by Perl. Basically, the 'fraction' methods will work for anything (as long as Perl can divide by it), but we would be wasting time if we only want to round to a certain decimal place.

Fractional Formats:

Anything which does not reduce to a 'multiple of 10'.

0.125 0.00007 2 2.885

Decimal Formats:

Anything which can be expressed as 1.0e<foo>.

0.0000001 1.0e-10

Number-of-Digits (Decimal) Format:

Anything which matches the /^d\d+$/ pattern will be used as a 'digit count'.

d0 d5 d60 # bad idea, but valid

Fake Format:

Anything less than zero.

-1 -0.001

Creates a new decimal-based rounding object.

Math::Round::Var::Float->new(precision => 7);

The argument to precision is the number of digits to use in rounding. This is used as part of a sprintf() format.

$number = $rounder->round($number);

Math::Round::Var::Fraction->new();

$number = $rounder->round($number);

This mode doesn't round at all. This is useful when you need user-input to be able to disable rounding without rewriting a lot of code.

Math::Round::Var::Fake->new();

$fake->round();

syntax highlighting: