CFLib.org – Common Function Library Project

PercentageChange(var1, var2)

Last updated July 03, 2002

author

Guillermo Cruz

Version: 1 | Requires: CF5 | Library: MathLib

Description:
This function can be used with any 2 numbers you want the percentage change on. Imagine two numbers that pertain to someone's weight. The first number is what they use to weigh and the second is what they currently weigh. You want to know the percentage difference with the + or minus attached to the outcome.

Return Values:
Returns a string.

Example:

<cfoutput>
PercentageChange(210,175) = #PercentageChange(210,175)#
</cfoutput>

Parameters:

Name Description Required
var1 The first number. Yes
var2 The second number. Yes

Full UDF Source:

/**
 * Check the percentage change between 2 numbers.
 * 
 * @param var1      The first number. (Required)
 * @param var2      The second number. (Required)
 * @return Returns a string. 
 * @author Guillermo Cruz (gcruz@elkiwa.com) 
 * @version 1, July 3, 2002 
 */
function PercentageChange(var1,var2){            
    var maxNumber = max(var1,var2);
    var minNumber = min(var1,var2);
    var change = maxNumber - minNumber;
    var symbol = "";
        
    if (var1 EQ var2)   return 0;
    change = NumberFormat(change / var1 * 100, 0.00);    
    
    if(var1 GT var2) symbol = "-";
    else symbol = "+";

    return symbol & " " & change;    
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson