joyser Posted March 22, 2010 Share Posted March 22, 2010 I have the following: $decimal is a number above 1.0. function change_number($decimal){ $decimal--; $divider=$decimal; $q=1; while($new_decimal != $decimal) { $decimal = $divider*$q; $new_decimal = explode(".",$decimal); $new_decimal = $new_decimal[0]; $q++; } return $decimal."/".$decimal/$divider; } Anytime I have a number under 1.5 as $decimal, the while() loop does not function properly? any ideas? Link to comment https://forums.phpfreaks.com/topic/196118-issue-with-numbers-under-5/ Share on other sites More sharing options...
scvinodkumar Posted March 22, 2010 Share Posted March 22, 2010 could u plz tell me what this function will do with decimal numbers Link to comment https://forums.phpfreaks.com/topic/196118-issue-with-numbers-under-5/#findComment-1029959 Share on other sites More sharing options...
joyser Posted March 22, 2010 Author Share Posted March 22, 2010 it will change a decimal number into a fraction, but of the simplest form, so the decimal of 1.5, would be brought down to .5 firstly, and then transformed into a fraction, 1/2. however, when I use any number below 1.5 (which is then .5) it doesn't work. Link to comment https://forums.phpfreaks.com/topic/196118-issue-with-numbers-under-5/#findComment-1029963 Share on other sites More sharing options...
maca134 Posted March 22, 2010 Share Posted March 22, 2010 so do you want the follow: 1.5 -> 3/2 or 1 1/2 2.2 -> 11/5 or 2 1/5 ?? Link to comment https://forums.phpfreaks.com/topic/196118-issue-with-numbers-under-5/#findComment-1029970 Share on other sites More sharing options...
joyser Posted March 22, 2010 Author Share Posted March 22, 2010 1.5 -> 3/2 2.2 -> 11/5 sorry, ignore the $decimal--; at the top Link to comment https://forums.phpfreaks.com/topic/196118-issue-with-numbers-under-5/#findComment-1029971 Share on other sites More sharing options...
maca134 Posted March 22, 2010 Share Posted March 22, 2010 found this: http://www.zend.com//code/codex.php?ozid=175&single=1 Slightly edited it: class dec2fraction { function convert($number, $top_heavy = false) { if (strpos($number, '.') === false) $number .= '.0'; list ($whole, $numerator) = explode ('.', $number); $denominator = 1 . str_repeat (0, strlen ($numerator)); $GCD = self::GCD($numerator, $denominator); $numerator /= $GCD; $denominator /= $GCD; if ($top_heavy) { $numerator = $numerator + ($whole * $denominator); $whole = 0; } return array('whole' => (int) $whole, 'numerator' => $numerator, 'denominator' => $denominator); } function GCD ($a, $b) { while ( $b != 0) { $remainder = $a % $b; $a = $b; $b = $remainder; } return abs ($a); } } var_dump(dec2fraction::convert(3.14, true)); var_dump(dec2fraction::convert(3.14, false)); var_dump(dec2fraction::convert(3, true)); var_dump(dec2fraction::convert(3, false)); var_dump(dec2fraction::convert(5.25, true)); var_dump(dec2fraction::convert(5.25, false)); /* OUTPUT array 'whole' => int 0 'numerator' => int 157 'denominator' => int 50 array 'whole' => int 3 'numerator' => int 7 'denominator' => int 50 array 'whole' => int 0 'numerator' => int 3 'denominator' => int 1 array 'whole' => int 3 'numerator' => int 0 'denominator' => int 1 array 'whole' => int 0 'numerator' => int 21 'denominator' => int 4 array 'whole' => int 5 'numerator' => int 1 'denominator' => int 4 */ Hope this helps Link to comment https://forums.phpfreaks.com/topic/196118-issue-with-numbers-under-5/#findComment-1029977 Share on other sites More sharing options...
joyser Posted March 22, 2010 Author Share Posted March 22, 2010 yup thats perfect thanks Link to comment https://forums.phpfreaks.com/topic/196118-issue-with-numbers-under-5/#findComment-1029979 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.