Last active
January 10, 2023 23:15
-
-
Save thedavecarroll/458429ec442b109ea8d6ba46b9ed7b18 to your computer and use it in GitHub Desktop.
IronScripter Challenge - November 15, 2019 - Beginner PowerShell Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ConvertTo-Celsius { | |
param($Fahrenheit) | |
($Fahrenheit - 32) * 5/9 | |
} | |
function ConvertTo-Fahrenheit { | |
param($Celsius) | |
($Celsius * 9/5) + 32 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ConvertTo-Celsius { | |
param($Fahrenheit) | |
process { | |
[PsCustomObject]@{ | |
Fahrenheit = $Fahrenheit | |
Celsius = ($Fahrenheit - 32) * 5/9 | |
} | |
} | |
} | |
function ConvertTo-Fahrenheit { | |
param($Celsius) | |
process { | |
[PsCustomObject]@{ | |
Celsius = $Celsius | |
Fahrenheit = ($Celsius * 9/5) + 32 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ConvertTo-Celsius { | |
param( | |
[Parameter(ValueFromPipeline,Position=0)] | |
[decimal[]]$Fahrenheit, | |
[ValidateRange(0,28)] | |
[int]$Precision = 1 | |
) | |
process { | |
foreach ($Temperature in $Fahrenheit) { | |
if ($Temperature -ge -459.67) { | |
[PsCustomObject]@{ | |
Fahrenheit = $Temperature | |
Celsius = [math]::Round(($Temperature - 32) * 5/9,$Precision) | |
} | |
} else { | |
'{0} {1}F is below absolute zero. Please try again.' -f $Temperature,[char]176 | Write-Warning | |
} | |
} | |
} | |
} | |
function ConvertTo-Fahrenheit { | |
param( | |
[Parameter(ValueFromPipeline,Position=0)] | |
[decimal[]]$Celsius, | |
[ValidateRange(0,28)] | |
[int]$Precision = 1 | |
) | |
process { | |
foreach ($Temperature in $Celsius) { | |
if ($Temperature -ge -273.15) { | |
[PsCustomObject]@{ | |
Celsius = $Temperature | |
Fahrenheit = [math]::Round(($Temperature * 9/5) + 32,$Precision) | |
} | |
} else { | |
'{0} {1}F is below absolute zero. Please try again.' -f $Temperature,[char]176 | Write-Warning | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Temperature { | |
[decimal]$Value | |
[ValidateSet('F','C','K')] | |
[string]$Unit | |
hidden [string]$Comment | |
Temperature () {} | |
Temperature ( $Value, $Unit) { | |
$BelowAbsoluteZero = switch ($Unit) { | |
'F' { if ($Value -lt -459.67) { $true } } | |
'C' { if ($Value -lt -273.15) { $true } } | |
'K' { if ($Value -lt 0) { $true } } | |
default { $false } | |
} | |
if ($BelowAbsoluteZero) { | |
if ($Unit -match 'F|C' ) { | |
throw ('{0} {1}{2} is below absolute zero. Please try again.' -f $Value,[char]176,$Unit) | |
} else { | |
throw ('{0} {1} is below absolute zero. Please try again.' -f $Value,$Unit) | |
} | |
} | |
$this.Unit = $Unit | |
$this.Value = $Value | |
$this.Comment = $this.GetStateChangeComment() | |
} | |
[string] ToString () { | |
if ($this.Unit -match 'F|C') { | |
return '{0} {1}{2}' -f $this.Value,[char]176,$this.Unit | |
} else { | |
return '{0} {1}' -f $this.Value,$this.Unit | |
} | |
} | |
[decimal] ToFahrenheit () { | |
$ToFahrenheit = switch ($this.Unit) { | |
'C' { ($this.Value * 9/5) + 32 } | |
'K' { ($this.Value - 273.15) * 9/5 + 32 } | |
default { $this.Value } | |
} | |
return $ToFahrenheit | |
} | |
[decimal] ToFahrenheit ([int]$Precision) { | |
$ToFahrenheit = switch ($this.Unit) { | |
'C' { ($this.Value * 9/5) + 32 } | |
'K' { ($this.Value - 273.15) * 9/5 + 32 } | |
default { $this.Value } | |
} | |
return [decimal]::Round($ToFahrenheit,$Precision) | |
} | |
[string] ToFahrenheitString () { | |
return '{0} {1}{2}' -f $this.ToFahrenheit(),[char]176,'F' | |
} | |
[string] ToFahrenheitString ([int]$Precision) { | |
return '{0} {1}{2}' -f $this.ToFahrenheit($Precision),[char]176,'F' | |
} | |
[decimal] ToCelsius () { | |
$ToCelsius = switch ($this.Unit) { | |
'F' { ($this.Value - 32) * 5/9 } | |
'K' { $this.Value - 273.15 } | |
default { $this.Value } | |
} | |
return $ToCelsius | |
} | |
[decimal] ToCelsius ([int]$Precision) { | |
$ToCelsius = switch ($this.Unit) { | |
'F' { ($this.Value - 32) * 5/9 } | |
'K' { $this.Value - 273.15 } | |
default { $this.Value } | |
} | |
return [decimal]::Round($ToCelsius,$Precision) | |
} | |
[string] ToCelsiusString () { | |
return '{0} {1}{2}' -f $this.ToCelsius(),[char]176,'C' | |
} | |
[string] ToCelsiusString ([int]$Precision) { | |
return '{0} {1}{2}' -f $this.ToCelsius($Precision),[char]176,'C' | |
} | |
[decimal] ToKelvin () { | |
$ToKelvin = switch ($this.Unit) { | |
'F' { ($this.Value - 32) * 5/9 + 273.15 } | |
'C' { $this.Value + 273.15 } | |
default { $this.Value } | |
} | |
return $ToKelvin | |
} | |
[decimal] ToKelvin ([int]$Precision) { | |
$ToKelvin = switch ($this.Unit) { | |
'F' { ($this.Value - 32) * 5/9 + 273.15 } | |
'C' { $this.Value + 273.15 } | |
default { $this.Value } | |
} | |
return [decimal]::Round($ToKelvin,$Precision) | |
} | |
[string] ToKelvinString () { | |
return '{0} {1}' -f $this.ToKelvin(),'K' | |
} | |
[string] ToKelvinString ([int]$Precision) { | |
return '{0} {1}' -f $this.ToKelvin($Precision),'K' | |
} | |
[object] Convert () { | |
return [PsCustomObject]@{ | |
Fahrenheit = $this.ToFahrenheit() | |
Celsius = $this.ToCelsius() | |
Kelvin = $this.ToKelvin() | |
Comment = $this.Comment | |
} | |
} | |
[object] Convert ([int]$Precision) { | |
return [PsCustomObject]@{ | |
Fahrenheit = $this.ToFahrenheit($Precision) | |
Celsius = $this.ToCelsius($Precision) | |
Kelvin = $this.ToKelvin($Precision) | |
Comment = $this.Comment | |
} | |
} | |
[object] ConvertWithString () { | |
return [PsCustomObject]@{ | |
Fahrenheit = $this.ToFahrenheit() | |
FahrenheitString = $this.ToFahrenheitString() | |
Celsius = $this.ToCelsius() | |
CelsiusString = $this.ToCelsiusString() | |
Kelvin = $this.ToKelvin() | |
KelvinString = $this.ToKelvinString() | |
Comment = $this.Comment | |
} | |
} | |
[object] ConvertWithString ([int]$Precision) { | |
return [PsCustomObject]@{ | |
Fahrenheit = $this.ToFahrenheit($Precision) | |
FahrenheitString = $this.ToFahrenheitString($Precision) | |
Celsius = $this.ToCelsius($Precision) | |
CelsiusString = $this.ToCelsiusString($Precision) | |
Kelvin = $this.ToKelvin($Precision) | |
KelvinString = $this.ToKelvinString($Precision) | |
Comment = $this.Comment | |
} | |
} | |
[string] GetStateChangeComment () { | |
if ($this.ToCelsius() -eq -273.15) { | |
$GetStateChangeComment = 'Absolute Zero' | |
} else { | |
$Celsius = $this.ToCelsius(0) | |
$GetStateChangeComment = switch ($Celsius) { | |
0 { 'Freezing Point' ; break } | |
3 { 'Near Freezing Point' ; break } | |
100 { 'Boiling Point' ; break } | |
233 { 'Fahrenheit 451 – the temperature at which book paper catches fire, and burns...' ; break } | |
5505 { 'Surface of the Sun' ; break } | |
} | |
} | |
return $GetStateChangeComment | |
} | |
} |
Catching a value below absolute zero is a great call. I wouldn't have thought of that.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Beginner PowerShell Functions - Fahrenheit/Celsius Conversion
This gist is my response to the IronScripter Beginner PowerShell Function challenge.
Simple Functions
The first two functions assumes you will provide some form of number (integer, byte, decimal).
Intermediate Functions
The intermediate functions takes the supplied parameter and returns a
PSCustomObject
with two properties, one for the original temperature and another for the converted temperature.In order to allow a negative number, the calculation converts the supplied temperature to a
System.Decimal
type.Advanced Functions
The two advanced functions can take in an array of
System.Decimal
objects from the pipeline (or via the parameter) and allows the user to specify the precision (what decimal point to round to). Additionally, if the temperature provided is below absolute zero, a warning is generated. The functions returns an object for each valid temperature that has the original temperature and another for the converted temperature.Temperature Class
Once I completed the three levels of functions, I wanted to create a PowerShell custom class,
[Temperature]
.The basic constructor requires two parameters, a
[decimal]
and a[string]
, which must be either 'C', 'F', or 'K' to signify a specific measurement unit.Methods
Within the class definition, several methods provides the user options for converting and displaying temperatures.
[string]
of the value, unit, and the degree symbol (except for K)[decimal]
of the original value, converted to Fahrenheit[int] Precision
[string]
of the value converted to Fahrenheit, unit, and the degree symbol[int] Precision
[decimal]
of the original value, converted to Celsius[int] Precision
[string]
of the value converted to Celsius, unit, and the degree symbol[int] Precision
[decimal]
of the original value, converted to Kelvin[int] Precision
[string]
of the value converted to Kelvin and unit[int] Precision
[PSCustomObject]
with the Fahrenheit, Celsius, Kelvin, and Comment[int] Precision
[PSCustomObject]
with the Fahrenheit, Celsius, Kelvin, their string representations, and Comment[int] Precision
[string]
describing the temperature, such as boiling pointDescription of the Temperature
The description of several significant points for the temperature are stored in the hidden
Comment
property.You can also call the
GetStateChangeComment()
method to see the Comment.Multiple Conversions
You can call the
ConvertWithString()
method to see all of the temperatures with their string equivalents.By calling the
ToCelsiusString()
andToFahrenheitString()
methods inline, you could present the three temperatures at the same time.Summary
Temperature conversion is very common in getting someone familiar with new programming or scripting language. I remember doing something similar for Commodore 64 in the mid-80's.
I had fun building the PowerShell custom class and learned a few things along the way.
Please feel free to leave a comment.