Need to use two function in a single line. i.e. (Divide and multiplication should be in the same line of $result).
function Percentage([float]$a,[float]$b){
$result = divide $a $b
$result = multiply $result 100
return $result
}
Need to use two function in a single line. i.e. (Divide and multiplication should be in the same line of $result).
function Percentage([float]$a,[float]$b){
$result = divide $a $b
$result = multiply $result 100
return $result
}
Hello @maunilkshatriya,
You can use $() subexpression:
function Percentage([float]$a,[float]$b){
return multiply $(divide $a $b) 100
}
Hope that helps.
even without subexpressions too
multiply (divide $a $b) 100
[string]
formatted as Percent when -ShowSign
is declared.5
) to control accuracy of result.5.1.14409.1018
Tip: PowerShell 7.1 allows param attribute [ValidateRange("Positive")]
. This could replace the need for a try\catch
on the denominator value (not shown).
function Get-Percentage([float]$num,[float]$denom,[switch]$ShowSign)
{
switch ($ShowSign)
{
$true {[decimal]::Round($num/$denom,5).ToString("p"); break}
$false {100*([decimal]::Round($num/$denom,5))}
}
}
# test
[float]$a = 50; [float]$b = 100;
[float]$i = 7.654; [float]$j = 35.343;
cls;
Get-Percentage $a $b -ShowSign;
Get-Percentage $i $j -ShowSign;
Get-Percentage $i $j;
# results
50.00 %
21.66 %
21.65600