This is my function for finding the square of a value:
function Get-Square($value)
{
$result = $value * $value
return $result
}
$value = Read-Host 'Enter a value'
$result = Get-Square $value
Write-Output "$value * $value = $result"
PS C:\Users> .\Get-Time.ps1 Enter a value: 4 4 * 4 = 4444
Why is the result 4444 and not 16? Thank you.
In addition to the Alistair's answer about converting, the string
returned by Read-Host
to int
, you might want to use the Math library to square the value.
function Get-Square([int] $value)
{
$result = [Math]::Pow($value,2)
return $result
}
$value = Read-Host 'Enter a value'
$result = Get-Square $value
Write-Output "$value * $value = $result"
Enter a value: 4
4 * 4 = 16
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With