Kazuki Posted April 17, 2009 Share Posted April 17, 2009 I'm trying to get a function to display Improper fraction as a mixed number. if I do this it seems to work. <?php echo mkfraction(12,5,0,true,true); ?> But this dose not work. <?php echo mkfraction(102,5,0,true,true); ?> Here is the code: <?php //Make fractions in html by Kazuki function mkfraction($numerator,$denominator,$number=null,$altver=false,$fiximproper=true) { if($altver!==true||$altver!==false) { $altver = false; } if(!is_numeric($numerator)) { $numerator = 0; } if(!is_numeric($denominator)) { $denominator = 0; } if(!is_numeric($number)) { $number = null; } if($fiximproper===true&&$numerator>=$denominator) { if($numerator % $denominator) { if($number!==null&&$number!==0) { $oldnumber = $number; $number = ($numerator % $denominator); $numerator = $numerator - ($number * $denominator); $number += $oldnumber; } if($number===null||$number===0) { $number = $numerator % $denominator; $numerator = $numerator - ($number * $denominator); } } if(!($numerator % $denominator)) { if($number!==null&&$number!==0) { $number = ($numerator / $denominator) + $number; } if($number===null||$number===0) { $number = $numerator / $denominator; } $numerator = 0; } } if($numerator>0) { if($altver==true&&$number!==null) { return "<span style=\"font-size: 16px;\">".$number."</span> <span style=\"font-size: 10px;\"><sup>$numerator</sup></span><span style=\"font-size: 16px;\">⁄</span><span style=\"font-size: 10px;\"><sub>$denominator</sub></span>"; } if($altver==true&&$number===null) { return "<span style=\"font-size: 10px;\"><sup>$numerator</sup></span><span style=\"font-size: 16px;\">⁄</span><span style=\"font-size: 10px;\"><sub>$denominator</sub></span>"; } if($altver==false&&$number!==null) { return "<span style=\"font-size: 16px;\">".$number."</span> <span style=\"font-size: 10px;\"><sup>$numerator</sup></span><span style=\"font-size: 16px;\">/</span><span style=\"font-size: 10px;\"><sub>$denominator</sub></span>"; } if($altver==false&&$number===null) { return "<span style=\"font-size: 10px;\"><sup>$numerator</sup></span><span style=\"font-size: 16px;\">/</span><span style=\"font-size: 10px;\"><sub>$denominator</sub></span>"; } } if($numerator===0) { return "<span style=\"font-size: 16px;\">".$number."</span>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/154482-solved-improper-fractions/ Share on other sites More sharing options...
Daniel0 Posted April 17, 2009 Share Posted April 17, 2009 <?php $numerator = 5; $denominator = 3; $num = (int) ($numerator / $denominator); $newNumerator = $numerator - $num * $denominator; echo $numerator . '/' . $denominator . ' = '; if ($num == 0) { echo $numerator . '/' . $denominator; } else if ($newNumerator > 0) { echo $num . ' ' . $newNumerator . '/' . $denominator; } else { echo $num; } ?> Adjust aesthetics as you see fit. Link to comment https://forums.phpfreaks.com/topic/154482-solved-improper-fractions/#findComment-812307 Share on other sites More sharing options...
Kazuki Posted April 17, 2009 Author Share Posted April 17, 2009 Thank you very much. Link to comment https://forums.phpfreaks.com/topic/154482-solved-improper-fractions/#findComment-812311 Share on other sites More sharing options...
Daniel0 Posted April 17, 2009 Share Posted April 17, 2009 Improved version: function generateFraction($numerator, $denominator, $mixed = true) { if ((int) $denominator == 0) { throw new InvalidArgumentException('Cannot divide by zero.'); } if (!$mixed) { return (int) $numerator . '/' . (int) $denominator; } $num = (int) ($numerator / $denominator); $newNumerator = $numerator - $num * $denominator; if ($newNumerator < 0 && $denominator < 0) { $newNumerator *= -1; $denominator *= -1; } if ($num == 0) { return $numerator . '/' . $denominator; } else if ($newNumerator != 0) { return $num . ' + ' . $newNumerator . '/' . $denominator; } else { return $num; } } echo '5/3 = ' . generateFraction(5, 3) . PHP_EOL; echo '5/-5 = ' . generateFraction(5, -4) . PHP_EOL; echo '5/5 = ' . generateFraction(5, 5) . PHP_EOL; echo '-5/-5 = ' . generateFraction(-5, -5) . PHP_EOL; Changelog: 1) Made it into a function. 2) Made sure that the denominator cannot be zero. 3) Adds a plus sign between the integer part and fraction part to be more mathematically correct. 4) Now supports negative nominators and denominators. Output of above code: 5/3 = 1 + 2/3 5/-5 = -1 + 1/-4 5/5 = 1 -5/-5 = 1 Link to comment https://forums.phpfreaks.com/topic/154482-solved-improper-fractions/#findComment-812328 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.