mattm1712 Posted October 18, 2012 Share Posted October 18, 2012 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/269650-decimal-to-fraction-converter-help/ Share on other sites More sharing options...
JohnTipperton Posted October 19, 2012 Share Posted October 19, 2012 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/269650-decimal-to-fraction-converter-help/#findComment-1386196 Share on other sites More sharing options...
ManiacDan Posted October 19, 2012 Share Posted October 19, 2012 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) Quote Link to comment https://forums.phpfreaks.com/topic/269650-decimal-to-fraction-converter-help/#findComment-1386209 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.