Math::PercentChange - calculate the percent change between two values
use Math::PercentChange qw(percent_change); my $from = 10; my $to = 5; my $diff = percent_change($from, $to); # -50 use Math::PercentChange qw(f_percent_change); my $from = 10; my $to = 15; my $diff = f_percent_change($from, $to, "%.03f"); # 50.000% # or my $diff = f_percent_change($from, $to, "%.03f", 1); # 50.000
Calculate the percent change between two values. Returns the percent difference.
Calculate the percent change. Returns a dualvar. When used in numeric context, returns an unformatted percentage value. When used in string context, returns a formatted sprintf value.
Formatting options for sprintf can be passed as a third argument. If no formatting option is passed, the default rounds to two decimal places ("%.2f") and appends a percent sign.
Passing a fourth argument (1) will prevent the routine from appending a percent sign.
Mike Baas <email@example.com>
This extremely simple code was taken from Mark Jason Dominus' correction of David Wheeler's blog post on the subject matter of 'How To Calculate Percentage Change Between Two Values'. See http://www.justatheory.com/learn/math/percent_change.html for the original posting.