shamuraq Posted January 15, 2013 Share Posted January 15, 2013 Hi guys, I am trying to convert randomized float to fractions. For eg; 66.666666666667 = 66 2/3 Any pointers? Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 17, 2013 Share Posted January 17, 2013 (edited) I found a similar function online and changed it for the better. You can run this to test the function out. <?php $number = trim($_POST['number']); $denominator = trim($_POST['denominator']); if(!isset($_POST['denominator'])){ $denominator = 32; } ?> <html> <body> <form action="" method="post"> Number: <input type="text" name="number" value="<?php echo $number;?>"> Highest denominator: <select name="denominator"> <option VALUE="<?php echo $denominator; ?>"><?php echo $denominator; ?></option> <option VALUE="2"> 2</option> <option VALUE="3"> 3</option> <option VALUE="4"> 4</option> <option VALUE="8"> 8</option> <option VALUE="16"> 16</option> <option VALUE="24"> 24</option> <option VALUE="32"> 32</option> <option VALUE="48"> 48</option> <option VALUE="64"> 64</option> <option VALUE="100"> 100</option> <option VALUE="1000"> 1000</option> </select> <input type="submit"> </form> </body> </html> <?php function decimalToFraction($number, $max_denom=NULL) { //is there a number in it? if(!preg_match('#[0-9]#',$number)){ return $number; exit(); } else { //if is already a fraction, change back to decimal if(preg_match ('~/~', $number)){ $fraction = array('whole' => 0); preg_match('/^((?P<whole>\d+)(?=\s))?(\s*)?(?P<numerator>\d+)\/(?P<denominator>\d+)$/', $number, $fraction); $number = $fraction['whole'] + $fraction['numerator']/$fraction['denominator']; } $whole = floor($number); $decimal = $number - $whole; $denominators = array(2, 3, 4, 8, 16, 24, 32, 48, 64, 100, 1000);//add or remove highest denominations from array to your liking if(!is_null($max_denom) && is_numeric($max_denom)){ $leastCommonDenom = $max_denom; } else { $leastCommonDenom = max($denominators); } $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; } } if(isset($_POST['number']) && $number != ''){ $result = decimalToFraction($number, $denominator); if (preg_match('#[0-9]#',$result)){ echo $result; } else { echo "$result is not a number."; } }else { echo "Insert a number to convert to a fraction."; } ?> Edited January 17, 2013 by QuickOldCar Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 17, 2013 Share Posted January 17, 2013 A. Percentage is not the same as a fraction. B. It's a bit easier to go the other way. Quote Link to comment 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.