Jump to content

force math


aebstract

Recommended Posts

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

<?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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.