Created
December 10, 2022 15:12
-
-
Save Timberfang/222381680d3f4fc2e658ceb943bc93e4 to your computer and use it in GitHub Desktop.
Convert units of length, weight, and temperature.
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 Convert-Distance { | |
<# | |
.SYNOPSIS | |
Convert units of distance from metric to imperial and vise versa. | |
.DESCRIPTION | |
Convert units of distance from the input unit to the output unit. Supported units are the millimetre (mm), centimetre (cm), metre (m), kilometre (km), inch (in), foot (ft), | |
yard (yd), and mile (mi). | |
.PARAMETER Value | |
The number of units to convert. | |
.PARAMETER Unit | |
The unit used for the input type. Must be one of the following: in, ft, yd, mi, mm, cm, m, or km. | |
.EXAMPLE | |
Convert-Distance -Value 20 -Unit ft | |
.EXAMPLE | |
Convert-Distance -Value 50 -Unit in | |
.EXAMPLE | |
Convert-Distance 50 ft | Select-Object m | |
#> | |
[OutputType([PSCustomObject])] | |
param( | |
[Parameter(Mandatory,ValueFromPipeline,Position=0)] | |
[double] $Value, | |
[Parameter(Mandatory,Position=1)] | |
[ValidateSet('in', 'ft', 'yd', 'mi', 'mm', 'cm', 'm', 'km')] | |
[string] $Unit | |
) | |
process { | |
switch -Exact ($unit) { | |
{ 'in' -eq $_ } { | |
[PSCustomObject]@{ | |
mm = ($value * 25.4) | |
cm = ($value * 2.54) | |
m = ($value * 0.0254) | |
km = ($value * 0.0000254) | |
in = $value | |
ft = ($value / 12) | |
yd = ($value / 36) | |
mi = ($value / 63360) | |
} | |
} | |
{ 'ft' -eq $_ } { | |
[PSCustomObject]@{ | |
mm = ($value * 304.8) | |
cm = ($value * 30.48) | |
m = ($value * 0.3048) | |
km = ($value * 0.0003048) | |
in = ($value * 12) | |
ft = $value | |
yd = ($value / 3) | |
mi = ($value / 5280) | |
} | |
} | |
{ 'yd' -eq $_ } { | |
[PSCustomObject]@{ | |
mm = ($value * 914.4) | |
cm = ($value * 91.44) | |
m = ($value * .9144) | |
km = ($value * 0.0009144) | |
in = ($value * 36) | |
ft = ($value * 3) | |
yd = $value | |
mi = ($value / 1760) | |
} | |
} | |
{ 'mi' -eq $_ } { | |
[PSCustomObject]@{ | |
mm = ($value * 1609344) | |
cm = ($value * 160934.4) | |
m = ($value * 1609.344) | |
km = ($value * 1.609344) | |
in = ($value * 63360) | |
ft = ($value * 5280) | |
yd = ($value * 1760) | |
mi = $value | |
} | |
} | |
{ 'mm' -eq $_ } { | |
[PSCustomObject]@{ | |
in = ($value / 25.4) | |
ft = ($value / 304.8) | |
yd = ($value / 914.4) | |
mi = ($value / 1609344) | |
mm = $value | |
cm = ($value / 10) | |
m = ($value / 1000) | |
km = ($value / 1000000) | |
} | |
} | |
{ 'cm' -eq $_ } { | |
[PSCustomObject]@{ | |
in = ($value / 2.54) | |
ft = ($value / 30.48) | |
yd = ($value / 91.44) | |
mi = ($value / 160934.4) | |
mm = ($value * 10) | |
cm = $value | |
m = ($value / 100) | |
km = ($value / 100000) | |
} | |
} | |
{ 'm' -eq $_ } { | |
[PSCustomObject]@{ | |
in = ($value / 0.0254) | |
ft = ($value / 0.3048) | |
yd = ($value / 0.9144) | |
mi = ($value / 1609.344) | |
mm = ($value * 1000) | |
cm = ($value * 100) | |
m = $value | |
km = ($value / 1000) | |
} | |
} | |
{ 'km' -eq $_ } { | |
[PSCustomObject]@{ | |
in = ($value / 0.0000254) | |
ft = ($value / 0.0003048) | |
yd = ($value / 0.0009144) | |
mi = ($value / 1.609344) | |
mm = ($value * 1000000) | |
cm = ($value * 100000) | |
m = ($value * 1000) | |
km = $value | |
} | |
} | |
} | |
} | |
} | |
function Convert-Temperature { | |
<# | |
.SYNOPSIS | |
Convert units temperature units between farenheit, celcius, and kelvin. | |
.DESCRIPTION | |
Convert units of temperature from the input unit to the output unit. Supported units are farenheit (f), celcius (c), and kelvin (k) | |
.PARAMETER Value | |
The number of units to convert. | |
.PARAMETER Unit | |
The unit used for the input type. Must be one of the following: f, c, or k. | |
.EXAMPLE | |
Convert-Temperature -Value 20 -Unit f | |
.EXAMPLE | |
Convert-Temperature -Value 50 -Unit k | |
.EXAMPLE | |
Convert-Temperature 50 c | Select-Object k | |
#> | |
[OutputType([PSCustomObject])] | |
param( | |
[Parameter(Mandatory,ValueFromPipeline,Position=0)] | |
[double] $Value, | |
[Parameter(Mandatory,Position=1)] | |
[ValidateSet('f','c','k')] | |
[string] $Unit | |
) | |
process { | |
switch -Exact ($unit) { | |
{ 'f' -eq $_ } { | |
[PSCustomObject]@{ | |
c = (($value - 32) * (5 / 9)) | |
k = (($value + 459.67) * ( 5 / 9)) | |
} | |
} | |
{ 'c' -eq $_ } { | |
[PSCustomObject]@{ | |
f = (($value * ( 9 / 5)) + 32) | |
k = ($value + 273.15) | |
} | |
} | |
{ 'k' -eq $_ } { | |
[PSCustomObject]@{ | |
f = (($value * ( 9 / 5)) - 459.67) | |
c = ($value - 273.15) | |
} | |
} | |
} | |
} | |
} | |
function Convert-Weight { | |
<# | |
.SYNOPSIS | |
Convert units of weight from metric to imperial and vise versa. | |
.DESCRIPTION | |
Convert units of weight from the input unit to the output unit. Supported units are the ounce (oz), pound (lb), short ton (tn), milligram (mg), gram (g), kilogram (kg), and metric ton (t). | |
.PARAMETER Value | |
The number of units to convert. | |
.PARAMETER Unit | |
The unit used for the input type. Must be one of the following: oz, lb, tn, mg, g, kg, or t. | |
.EXAMPLE | |
Convert-Weight -Value 20 -Unit lb | |
.EXAMPLE | |
Convert-Weight -Value 50 -Unit oz | |
.EXAMPLE | |
Convert-Weight 50 kg | Select-Object tn | |
#> | |
[OutputType([PSCustomObject])] | |
param( | |
[Parameter(Mandatory,ValueFromPipeline,Position=0)] | |
[double] $Value, | |
[Parameter(Mandatory,Position=1)] | |
[ValidateSet('oz', 'lb', 'tn', 'mg', 'g', 'kg', 't')] | |
[string] $Unit | |
) | |
process { | |
switch -Exact ($unit) { | |
{ 'oz' -eq $_ } { | |
[PSCustomObject]@{ | |
mg = ($value * 28349.5231) | |
g = ($value * 28.3495231) | |
kg = ($value * 0.0283495231) | |
t = ($value * 0.0000283495231) | |
oz = $value | |
lb = ($value / 16) | |
tn = ($value / 32000) | |
} | |
} | |
{ 'lb' -eq $_ } { | |
[PSCustomObject]@{ | |
mg = ($value * 453592.37) | |
g = ($value * 453.59237) | |
kg = ($value * 0.45359237) | |
t = ($value * 0.00045359237) | |
oz = ($value * 16) | |
lb = $value | |
tn = ($value / 2000) | |
} | |
} | |
{ 'tn' -eq $_ } { | |
[PSCustomObject]@{ | |
mg = ($value * 907184740) | |
g = ($value * 907184.74) | |
kg = ($value * 907.18474) | |
t = ($value * 0.90718474) | |
oz = ($value * 32000) | |
lb = ($value * 2000) | |
tn = $value | |
} | |
} | |
{ 'mg' -eq $_ } { | |
[PSCustomObject]@{ | |
mg = $value | |
g = ($value / 1000) | |
kg = ($value / 1000000) | |
t = ($value / 1000000000) | |
oz = ($value / 28349.5231) | |
lb = ($value / 453592.37) | |
tn = ($value / 907184740) | |
} | |
} | |
{ 'g' -eq $_ } { | |
[PSCustomObject]@{ | |
mg = ($value * 1000) | |
g = $value | |
kg = ($value / 1000) | |
t = ($value / 1000000) | |
oz = ($value / 28.3495231) | |
lb = ($value / 453.59237) | |
tn = ($value / 907184.74) | |
} | |
} | |
{ 'kg' -eq $_ } { | |
[PSCustomObject]@{ | |
mg = ($value * 1000000) | |
g = ($value * 1000) | |
kg = $value | |
t = ($value / 1000) | |
oz = ($value / 0.0283495231) | |
lb = ($value / 0.45359237) | |
tn = ($value / 907.18474) | |
} | |
} | |
{ 't' -eq $_ } { | |
[PSCustomObject]@{ | |
mg = ($value * 1000000000) | |
g = ($value * 1000000) | |
kg = ($value * 1000) | |
t = $value | |
oz = ($value / 0.0000283495231) | |
lb = ($value / 0.00045359237) | |
tn = ($value / 0.90718474) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment