Convert Inches to Feet, and Inches

My blog serves several purposes. One of which I’ve mentioned before, is to keep things around I may want later. That’s why we’re back today. While helping on a TechNet forum post, I wrote the below function.

This function’s purpose is to convert inches between 56 and 76 to their <feet>'<inches>” equivalent. This means if you send in 56, you’ll get back 4’8″, if you send in 60 you’ll get 5’0″, if it’s 73 you’ll get 6’1″. The reason it’ll only take 56 to 76 inches is due to the initial requirements in the forum post. If you had a purpose for this function and wanted it to accept more or less inches, you’d have to modify the ValidateRange attribute to different minimum and maximum values, or remove it altogether. If you can use this function, then here it is.

Function Convert-InchToFeetInch {
    Param (
        [Parameter(Mandatory=$true)]
        [ValidateRange(56,76)]
        [int]$Inches
    )

    $TotalFeet = [int](($Inches / 12).ToString().Split('.')[0])
    $RemainingInches = $Inches - ($TotalFeet * 12)

    "$TotalFeet'$RemainingInches`""
}

This first example will loop through all the possible values from 56 to 76 using the ForEach-Object cmdlet. As it does that, it’ll write out the number of inches it’s converting, as well as the feet and remaining inches in the aforementioned format.

56..76 | ForEach-Object {
    "$_ inches is $(Convert-InchToFeetInch -Inches $_)"
}

56 inches is 4'8"
57 inches is 4'9"
58 inches is 4'10"
59 inches is 4'11"
60 inches is 5'0"
61 inches is 5'1"
62 inches is 5'2"
63 inches is 5'3"
64 inches is 5'4"
65 inches is 5'5"
66 inches is 5'6"
67 inches is 5'7"
68 inches is 5'8"
69 inches is 5'9"
70 inches is 5'10"
71 inches is 5'11"
72 inches is 6'0"
73 inches is 6'1"
74 inches is 6'2"
75 inches is 6'3"
76 inches is 6'4"

These next examples simply use the function outside a looping construct. You provide a number of inches to the Inches parameter, and the function will return the converted value. That’s it. Maybe it helps you, and maybe I can find it if I ever need it.

Convert-InchToFeetInch -Inches 56 # 4'8"
Convert-InchToFeetInch -Inches 60 # 5'0"
Convert-InchToFeetInch -Inches 68 # 5'8"
Convert-InchToFeetInch -Inches 73 # 6'1"
Convert-InchToFeetInch -Inches 76 # 6'4"

4'8"
5'0"
5'8"
6'1"
6'4"

Leave a Reply

Your email address will not be published. Required fields are marked *