Skip to content

Instantly share code, notes, and snippets.

@kevinCefalu
Last active January 24, 2020 20:30
Show Gist options
  • Save kevinCefalu/c57ff7ecdd85daea914262e466b3b8a3 to your computer and use it in GitHub Desktop.
Save kevinCefalu/c57ff7ecdd85daea914262e466b3b8a3 to your computer and use it in GitHub Desktop.
A function to invoke percentage calculations.
function Invoke-PercentCalculation
{
<#
.SYNOPSIS
Invoke a Percentage Calculation
.DESCRIPTION
Invoke a Percentage Calculation for one of the
three following calculation types:
- Find P percent of X
- Find what percent of X is Y
- Find X if P percent of it is Y
.PARAMETER X
Used in the following calculations:
- Find P percent of X
- Find what percent of X is Y
.PARAMETER Y
Used in the following calculations:
- Find what percent of X is Y
- Find X if P percent of it is Y
.PARAMETER Percent
Used in the following calculations:
- Find P percent of X
- Find X if P percent of it is Y
This parameter can be represented as a decimal or string. For
example, 0.1, '0.1', 10, '10', '10%' all represent a 10% value. If
this function receives anything other than a [string] that ends with
a '%' character the user will be asked which type the input value is
represented as, decimal or percent (i.e., 0.1 * 100 = 10% or 0.1% /
100 = 0.001, respectively). For unattended execution, pass a value
of 'Decimal' or 'Percent' to -PercentType.
.PARAMETER PercentType
Used in the following calculations:
- Find P percent of X
- Find X if P percent of it is Y
Including this optional parameter will allow for unattended
execution of this function. You can specify up front if -Percent
is being represented as a Decimal or a [whole] percent.
.OUTPUTS
[Decimal]
.EXAMPLE
PS> Invoke-PercentCalculation -Percent 0.1 -X 150 -Verbose;
Is 0.1 to be understood as a Decimal or Percent?
I.e., 0.1 * 100 = 10% or 0.1% / 100 = 0.001, respectively
[D] Decimal [P] Percent [?] Help (default is "D"): D
VERBOSE: Result: 10% of 150 is 15.0
15.0
.EXAMPLE
PS> Invoke-PercentCalculation -Percent 0.1 -X 150 -PercentType Decimal -Verbose;
VERBOSE: Result: 10% of 150 is 15.0
15.0
.EXAMPLE
PS> Invoke-PercentCalculation -Percent '0.1' -X 150 -Verbose;
Is 0.1 to be understood as a Decimal or Percent?
I.e., 0.1 * 100 = 10.0% or 0.1% / 100 = 0.001, respectively
[D] Decimal [P] Percent [?] Help (default is "D"): D
VERBOSE: Result: 10.0% of 150 is 15.0
15.0
.EXAMPLE
PS> Invoke-PercentCalculation -Percent '0.1' -X 150 -PercentType Decimal -Verbose;
VERBOSE: Result: 10% of 150 is 15.0
15.0
.EXAMPLE
PS> Invoke-PercentCalculation -Percent 10 -X 150 -Verbose;
Is 10 to be understood as a Decimal or Percent?
I.e., 10 * 100 = 1000% or 10% / 100 = 0.1, respectively
[D] Decimal [P] Percent [?] Help (default is "D"): P
VERBOSE: Result: 10% of 150 is 15.0
15.0
.EXAMPLE
PS> Invoke-PercentCalculation -Percent 10 -X 150 -PercentType Percent -Verbose;
VERBOSE: Result: 10% of 150 is 15.0
15.0
.EXAMPLE
PS> Invoke-PercentCalculation -Percent '10' -X 150 -Verbose;
Is 10 to be understood as a Decimal or Percent?
I.e., 10 * 100 = 1000% or 10% / 100 = 0.1, respectively
[D] Decimal [P] Percent [?] Help (default is "D"): P
VERBOSE: Result: 10.0% of 150 is 15.0
15.0
.EXAMPLE
PS> Invoke-PercentCalculation -Percent '10%' -X 150 -Verbose;
VERBOSE: Result: 10.0% of 150 is 15.0
15.0
.EXAMPLE
PS> Invoke-PercentCalculation -X 60 -Y 12 -Verbose;
VERBOSE: Result: 12 is 20.0% of 60
0.2
.EXAMPLE
PS> Invoke-PercentCalculation -Y 25 -Percent 0.20 -Verbose;
Is 0.2 to be understood as a Decimal or Percent?
I.e., 0.2 * 100 = 20% or 0.2% / 100 = 0.002, respectively
[D] Decimal [P] Percent [?] Help (default is "D"): D
VERBOSE: Result: 125 is 20% of 25
125
.EXAMPLE
PS> Invoke-PercentCalculation -Y 25 -Percent 0.20 -PercentType Decimal -Verbose;
VERBOSE: Result: 125 is 20% of 25
125
.EXAMPLE
PS> Invoke-PercentCalculation -Y 25 -Percent '0.20' -Verbose;
Is 0.2 to be understood as a Decimal or Percent?
I.e., 0.2 * 100 = 20% or 0.2% / 100 = 0.002, respectively
[D] Decimal [P] Percent [?] Help (default is "D"): D
VERBOSE: Result: 125 is 20% of 25
125
.EXAMPLE
PS> Invoke-PercentCalculation -Y 25 -Percent '0.20' -PercentType Decimal -Verbose;
VERBOSE: Result: 125 is 20% of 25
125
.EXAMPLE
PS> Invoke-PercentCalculation -Y 25 -Percent '20' -Verbose;
Is 20 to be understood as a Decimal or Percent?
I.e., 20 * 100 = 2000% or 20% / 100 = 0.2, respectively
[D] Decimal [P] Percent [?] Help (default is "D"): P
VERBOSE: Result: 125 is 20.0% of 25
125
.EXAMPLE
PS> Invoke-PercentCalculation -Y 25 -Percent '20' -PercentType Percent -Verbose;
VERBOSE: Result: 125 is 20.0% of 25
125
.EXAMPLE
PS> Invoke-PercentCalculation -Y 25 -Percent '20%' -Verbose;
VERBOSE: Result: 125 is 20.0% of 25
125
.NOTES
Kevin Cefalu, 2020-01-24
#>
[CmdletBinding()]
[OutputType([Decimal])]
param (
[Parameter(Mandatory, ParameterSetName = 'WhatIsPercentOfX')]
[Parameter(Mandatory, ParameterSetName = 'WhatPercentOfXIsY')]
[Decimal] $X,
[Parameter(Mandatory, ParameterSetName = 'WhatPercentOfXIsY')]
[Parameter(Mandatory, ParameterSetName = 'XIsPercentOfY')]
[Decimal] $Y,
[Parameter(Mandatory, ParameterSetName = 'WhatIsPercentOfX')]
[Parameter(Mandatory, ParameterSetName = 'XIsPercentOfY')]
$Percent,
[Parameter(ParameterSetName = 'WhatIsPercentOfX')]
[Parameter(ParameterSetName = 'XIsPercentOfY')]
[ValidateSet('Decimal', 'Percent')]
[string] $PercentType
);
[Decimal] $Result = $null;
switch -Regex ($PSCmdlet.ParameterSetName)
{
'WhatIsPercentOfX|XIsPercentOfY'
{
if ($Percent -is [string] -and $Percent -match '%')
{
$Percent = [Decimal]::Parse($Percent.Replace('%', ''));
$ShouldDivide = $true;
}
else
{
if ($Percent -is [string])
{
$Percent = [Decimal]::Parse($Percent);
}
if ($PSBoundParameters.ContainsKey('PercentType'))
{
$ShouldDivide = $PercentType -eq 'Percent';
}
else
{
$ChoiceType = [Management.Automation.Host.ChoiceDescription];
$ShouldDivide = ($Host.UI.PromptForChoice(
"Is $Percent to be understood as a Decimal or Percent?", (
"I.e., $Percent * 100 = $($Percent * 100)% or " +
"$Percent% / 100 = $($Percent / 100), respectively"
), @(
$ChoiceType::new('&Decimal', 'Decimal notation (i.e., 0.1 * 100 = 10%)'),
$ChoiceType::new('&Percent', 'Percent notation (i.e., 10% / 100 = 0.1')
), 0)) -as [Bool];
Write-Host;
}
}
if ($ShouldDivide)
{
$Percent = $Percent / 100;
}
}
'WhatIsPercentOfX'
{
$Result = $Percent * $X;
Write-Verbose "Result: $($Percent * 100)% of $X is $Result";
break;
}
'WhatPercentOfXIsY'
{
$Result = $Y / $X;
Write-Verbose "Result: $Y is $($Result * 100)% of $X";
break;
}
'XIsPercentOfY'
{
$Result = $Y / $Percent;
Write-Verbose "Result: $Result is $($Percent * 100)% of $Y";
break;
}
}
return $Result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment