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
Codes to calculate height
<?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
First Line
by dividing it by 2.54.
Second Line
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
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
First Line
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
Third Line
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.
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
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