Why you shouldn’t use google to convert heights and how you can fix it.

Explanation

If you’re trying to convert feet & inches to centimeters or vice versa using google’s “ft to cm” converter, then you should probably stop since it was never meant to convert “feet & inches to centimeters” or “centimeters to feet & inches”.

In this blog, I will be providing fixes for people trying to get an accurate measurement the quick way, and for my fellow coders/programmers who need the code version in order to implement it in their project.

An Example

Let’s say that I wanted to convert 5 feet and 7 inches to centimeters. The result that I’d get would  be “173.736 cm”

This is in no way wrong…if you follow google’s formula for the conversion that is. Google does not take the “inches” into consideration while calculating. According to google, the “.7” in my query does not represent  an “inch”, it just represents a mere decimal. It does not think of it as “7 inches”, it thinks of it as “5 feet and 70 decimals”.

The correct answer for my query would be “170.18 cm”

The Fix

I will be dividing this section into two segments, one is for people who just want a quick and accurate calculator and one will be for my fellow coders who would like the codes to convert the heights for their projects.

Quick and Accurate Calculator

If you would like to convert “feet & inches to centimeters” or “centimeters to feet & inches” and get accurate results, then I’d suggest you to visit CalculatorSoup.
It’s a simple website with a simple U.I that not only gives you an accurate result, but it also contains the formula needed to accurately convert heights.
 

Codes to calculate height

Here’s the code to convert “feet & inches to centimeters” and “centimeters to feet & inches”. This code is written in PHP, however I will explain the code and will guide you through it so that you can write your own versions in your own coding language.
 

<?php

//Function to convert centimeters to feet & inches. Credit to Akshay Hedge.

function cm2feet($cm)
{
     $inches = $cm/2.54;

     $feet = intval($inches/12);

     $inches = $inches%12;      

     $value = $feet.”.”.$inches;

     return $value;            // Return the value aka the result.
}

//For Example: $convertcentimeter = cm2feet(161);
//echo $convertcentimeter;
//Result = 5.3

//Function to convert feet & inches to centimeters. Credit to…. Me.

function feet2cm($ft)
{

     $explode = explode(“.”,$ft);

     $feet = $explode[0] * 30.48;

      $total = $feet;          

     if(isset($explode[1]))           
     {                             
     $inches = $explode[1];
     $inches = $inches * 2.54;
     $total = $feet + $inches;
     }

     return $total;                  
}

//For Example: $convertfeetandinches = feet2cm(5.3);
//echo $convertfeetandinches;
//Result = 160.02

?>

A quick explanation
Every sentence with a “$” sign in the beginning represents a variable.
For example $cm in javascript would be var cm.
 
Every sentence with “//” in the beginning represents a comment. It does not contribute to the code.
 
“echo” is a function used in PHP to display a variables value.
 
Cm2feet function
 
Let’s say that I wanted to convert 161 cm to feet & inches.
 
First Line
The first line in the function calculates and store the inches in a variable from the given centimeter
by dividing it by 2.54.
So 161 / 2.54 =  63.385826771654.
 
Second Line
The second line in the function calculates and store the feet in a variable by dividing the stored inches by 12 and retrieving it’s integer value.
So if inches =  63.385826771654, then  63.385826771654 / 12 = 5.2821522309712. Meaning that the feet = 5.
 
The intval() function in the line pretty much returns the integer from a float value. So in our example we got “5.2821522309712”, so it ignored everything after the “.” and returned 5.
 
Third Line
The third line in the function calculates the accurate inches the feet in a variable by getting the remainder of inches by 12.

So the inches we currently have =  63.385826771654, so   63.385826771654 % 12 = 3. Meaning that the inches = 3.

Fourth Line

The fourth line in the function combines both the feet and inches and stores them in a variable with a dot in between. For example value = 5.3 meaning 5 ft and 3 inches.

A javascript example: var value = feet+”.”+inches;   

Fifth Line

The fifth line simply returns the calculated value. Meaning that the value we got is 5.3     

Feet2cm function
 
Let’s say that I wanted to convert 5 feet & 3 inches or 5.3 ft to cm.
 
First Line
The first line in the code seperates the feet and inches and stores them in a variable. The explode() function in php breaks a string into an array by using a seperator. The “.” being the seperator. Meaning that the variable “$explode” is now an array that contains the feet “$explode[0]” and the inches “$explode[1]” if given.
 
Second Line
The second line in the function multiplies the feet by 30.48 and stores it in a variable. So: 5  * 30.48 = 152.4.
 
Third Line
The third line in the function stores the calculated feet in a variable.
 
Fourth Line

The fourth line in the function checks whether there were any inches during the input. So with our example : 5.3, 3 being the inch, if the inch exist then multiply the inch stored in the array by 2.54 and then add it with the calculated feet. So 3 * 2.54 = 7.62, therefore, 152.4 + 7.62 = 160.02.

The isset() function checks whether the given variable is set/declared or is empty. If the conversion that I wanted was only “5 feet”, then the feet side of the array would have been set, however, the inches side, meaning “$explode[1]” would not exist and hence, would create an issue. Which is why we check for it’s existence and if it does exist, add it to the already calculated feet’s value.
 
Fifth Line
The fifth line in the function simply returns the calculated value. Meaning that the value we got is 160.02
 

Conclusion

Well, that was pretty much it. I hope that I was able to help you regarding this issue. Do feel free to ask for any kind of assistance in the comments.

-Engineer From Commerce

Leave a Reply

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