Jump to content

Decimal To Fraction Converter Help


mattm1712

Recommended Posts

hi i have this code but having problem with reoccurring decimals say my datbase says its 0.3333333 which should be unlimited as its 1/3 but i cant have unlimited values in my database. i am suplied the value as 0.3333333 so i cant put 1/3 into my database so i have this formulat so far but i am having problems with it due to this, i need a way of replacing any value eg .33333 or .333333 or .333333333 to 1/3 any ideas ???

 

 

<?php

 

$price = 0.33333333;

$numer = $price*1000;

$denom = 1000;

$primes = array(41,37,31,29,23,19,17,13,11,7,5,3,2);

$factors = array(10,9,8,7,6,5,4,3,2);

$running = true;

while ($running)

{

$changed = false;

if (in_array($denom,$primes)&&$numer % $denom != 0)

{

$running = false;

}

else

{

foreach($factors as $fac)

{

if ($numer % $fac == 0 && $denom % $fac == 0)

{

$numer = $numer/$fac;

$denom = $denom/$fac;

$changed = true;

}

}

if (!$changed)

{

$running=false;

}

}

}

echo $numer."/".$denom;

 

?>

Link to comment
Share on other sites

hi i have this code but having problem with reoccurring decimals say my datbase says its 0.3333333 which should be unlimited as its 1/3 but i cant have unlimited values in my database. i am suplied the value as 0.3333333 so i cant put 1/3 into my database so i have this formulat so far but i am having problems with it due to this, i need a way of replacing any value eg .33333 or .333333 or .333333333 to 1/3 any ideas ???

 

 

<?php

 

$price = 0.33333333;

$numer = $price*1000;

$denom = 1000;

$primes = array(41,37,31,29,23,19,17,13,11,7,5,3,2);

$factors = array(10,9,8,7,6,5,4,3,2);

$running = true;

while ($running)

{

$changed = false;

if (in_array($denom,$primes)&&$numer % $denom != 0)

{

$running = false;

}

else

{

foreach($factors as $fac)

{

if ($numer % $fac == 0 && $denom % $fac == 0)

{

$numer = $numer/$fac;

$denom = $denom/$fac;

$changed = true;

}

}

if (!$changed)

{

$running=false;

}

}

}

echo $numer."/".$denom;

 

?>

 

try this code bro i created a function to be able to output the fraction form.

 

<?php
function decToFraction($float) {
   // 1/2, 1/4, 1/8, 1/16, 1/3 ,2/3, 3/4, 3/8, 5/8, 7/8, 3/16, 5/16, 7/16,
   // 9/16, 11/16, 13/16, 15/16
   $whole = floor ( $float );
   $decimal = $float - $whole;
   $leastCommonDenom = 48; // 16 * 3;
   $denominators = array (2, 3, 4, 8, 16, 24, 48 );
   $roundedDecimal = round ( $decimal * $leastCommonDenom ) / $leastCommonDenom;
   if ($roundedDecimal == 0)
    return $whole;
   if ($roundedDecimal == 1)
    return $whole + 1;
   foreach ( $denominators as $d ) {
    if ($roundedDecimal * $d == floor ( $roundedDecimal * $d )) {
	    $denom = $d;
	    break;
    }
   }
   return ($whole == 0 ? '' : $whole) . " " . ($roundedDecimal * $denom) . "/" . $denom;
}
echo decToFraction(0.5);
?>

Link to comment
Share on other sites

Why do you need more than 10 decimal places in your database? At some point, you have to be reasonable. There are some fractions which cannot be represented as decimals, and some repeating decimals which are very difficult to detect.

 

Functions like the one you were given only solve a subset of the problem. It doesn't handle x/5, x/7, x/11, or x/13, and those are just the primes below 16 (its max)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.