What this script does is take a number in decimal form (ex: 300.67) and converts it into a fraction for the imperial measuring system.
I looked for an already made script but to no avail so I made my own.
class Belts
{
var $length;
var $numerator = '';
var $reduce = TRUE;
var $denominator = 16;
function decToFrac()
{
list ($whole, $numerator) = explode ('.', $this->length);
$newlength = floatval('.'.$numerator) * $this->denominator;
if($newlength > 0)
{
list ($floatwhole, $floatnumerator) = explode ('.', $newlength);
$floatwhole = "$floatwhole.$floatnumerator";
$floatwhole = floatval($floatwhole);
$floatwhole = round($floatwhole);
if((!$floatwhole&1))
{
if($floatwhole != 0){
$gcd = $this->gcd($floatwhole, $this->denominator);
$divnumerator = $floatwhole / $gcd;
}
}
else
{
$divnumerator = $floatwhole;
}
if($floatwhole != 0)
{
while($this->reduce == TRUE)
{
$divnumerator = $divnumerator / 2;
$this->denominator = $this->denominator / 2;
if ($this->check_float("$divnumerator")){
//echo "TEST: $divnumerator";
$divnumerator = $divnumerator * 2;
$this->denominator = $this->denominator * 2;
$this->reduce = FALSE;
}
}
}
}
/*echo "GCD: $gcd<br />
Numerator: $divnumerator<br />
Denominator: $divdenominator";*/
//die($whole);
if($whole >= 12)
$whole = $whole / 12;
list ($feet, $inches) = explode ('.', $whole);
$inches = "0.$inches";
$inches = floatval($inches);
$inches = $inches * 12;
$inches = round($inches);
$divnumerator = round($divnumerator);
$feet = floor($feet);
if($whole < 12){
$inches = $feet;
$feet = "0";
}
$newlength = "$feet - $inches";
if($divnumerator > 0)
$newlength .= " <sup>$divnumerator</sup> / <sub>$this->denominator</sub>";
return $newlength;
}
function gcd($m,$n)
{
$_m=$m;$r=1;
if($m<$n){$t=$m;$m=$n;$n=$t;}
while($r)
{
$r=(floor($m/$n)*$n)-$m;
$_n=$n;$n=$r;$m=$_m;
}
return abs($_n);
}
function check_float($mVal)
{
return ( is_float($mVal)
|| ( (float) $mVal != round($mVal)
|| strlen($mVal) != strlen( (int) $mVal) )
&& $mVal != 0 );
}
}
Example of use
$belttech = new Belts;
//length always in inches
$belttech->length = 4.00;
echo $belttech->decToFrac();