aebstract Posted April 4, 2008 Share Posted April 4, 2008 I want to take a value like "5/16" from a form field and force it to be divided in to a fraction ".3125". What is the best way to take a variable such as $_POST['fraction'] and divide it in to that decimal .3125 ? Link to comment https://forums.phpfreaks.com/topic/99581-force-math/ Share on other sites More sharing options...
GingerRobot Posted April 4, 2008 Share Posted April 4, 2008 How's about: <?php $fraction = '5/16'; list($numerator,$denominator) = explode('/',$fraction); $decimal = $numerator/$denominator; echo $decimal; ?> Or we can juse use the eval() function: <?php $fraction = '5/16'; eval("\$decimal=$fraction;"); echo $decimal; ?> Link to comment https://forums.phpfreaks.com/topic/99581-force-math/#findComment-509438 Share on other sites More sharing options...
craygo Posted April 4, 2008 Share Posted April 4, 2008 <?php $fraction = "5/16"; $parts = $fraction = "5/16"; $parts = explode("/", $fraction); $dec = $parts[0] / $parts[1]; echo $dec; ?> Or make a function <?php function decimal($fraction){ $parts = explode("/", $fraction); $dec = $parts[0] / $parts[1]; return $dec; } echo decimal("5/16"); ?> Ray Link to comment https://forums.phpfreaks.com/topic/99581-force-math/#findComment-509440 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2008 Share Posted April 4, 2008 You can do something like: <?php list($num,$denom) = explode('/',$_POST['fraction']); $value = $num / $denom; echo number_format($value,4,'.',' '); ?> Ken (beaten to it...) Link to comment https://forums.phpfreaks.com/topic/99581-force-math/#findComment-509446 Share on other sites More sharing options...
aebstract Posted April 4, 2008 Author Share Posted April 4, 2008 thanks, I went with the following. One thing I see that is gonna be a problem, it's displaying as 0.### vs just .###. I need to learn about arrays and maybe this will help me, how can I get rid of the 0? $fraction = $_POST['dim1f']; list($numerator,$denominator) = explode('/',$fraction); $decimal1 = $numerator/$denominator; $fraction2 = $_POST['dim2f']; list($numerator2,$denominator2) = explode('/',$fraction2); $decimal2 = $numerator2/$denominator2; $dim1 = $_POST['dim1']; $dim2 = $_POST['dim2']; echo "$dim1$decimal1, $dim2$decimal2"; Link to comment https://forums.phpfreaks.com/topic/99581-force-math/#findComment-509465 Share on other sites More sharing options...
aebstract Posted April 4, 2008 Author Share Posted April 4, 2008 I just added the decimal to the whole number. I need to use it as one solid number anyway so this works. Link to comment https://forums.phpfreaks.com/topic/99581-force-math/#findComment-509470 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2008 Share Posted April 4, 2008 Since you're doing this more than once in your code, I would suggest using a function: <?php function decimalize($frac) { list($n,$d) = explode('/',$frac); return ($n/$d); } $dim = array(); for ($i=1;$i<3;$i++) { $dim[$i] = $_POST['dim'.$i] + decimalize($_POST['dim' . $i . 'f']); echo $dim[$i] . '<br>'; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/99581-force-math/#findComment-509508 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.