The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

SYNOPSIS

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";

DESCRIPTION

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.

AUTHOR

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

http://scratchcomputing.com

COPYRIGHT

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

LICENSE

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)

Front-End Constructor

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.

new

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

format_of

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

Decimal-based rounding

new

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.

round

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

Fraction-based rounding.

new

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

round

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

Fake rounding

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.

new

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

round

  $fake->round();