Jump to content

Converting decimal to percentage


shamuraq

Recommended Posts

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 by QuickOldCar
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.