Ali Ali - 24 days ago 6
MySQL Question

How can I save feet and inches in MySQL database?

function inchesTodecimal($inches){
return $decimal=inches/12;
}


I want convert inches to decimal like 11=0.917. I want to save 11 inches in database like decimal number 0.917.there is a problem occur when inches is 10.In float type .10=1

Answer Source

First you convert your feet Inches in to feet decimal like 5 feet and after point value convert it into decimal like 11=9.17 then insert into your database. When you want to show on user end you convert again feet Decimal to feet Inches like 5.917=>5.11 its simple hope you got what you want...

Here my simple code for conversion:

function feetInchesToDecimal($feetinches,$precision = 3){
   list($feet,$inches) = explode(".",$feetinches);
   $inches = preg_replace('/[^0-9.]/','',$inches);
   $inches = $inches + ($feet * 12);
   return round($inches /12,$precision);
}

function feetDecimalToInchs($feetdecimal){
    $feet=(int)$feetdecimal;
    $inches=round(($feetdecimal-$feet)*12);
    $feetinches=$feet;
    if($inches>0) $feetinches.=".".$inches;
    return $feetinches;
}

echo feetInchesToDecimal(5.11); //5.917
echo feetDecimalToInchs(5.917); //5.11