I have a string '1/2'
and i need to get a float value 0.5
from it.
Other examples:
Operation: $operation = '5/8';
Wanted value: $value = 0.625;
I have a string '1/2'
and i need to get a float value 0.5
from it.
Other examples:
Operation: $operation = '5/8';
Wanted value: $value = 0.625;
You can use eval
, also please note your are not looking for an int
as they are whole numbers.
<?php
$operation = '1/2';
eval('$value = ' . $operation . ';');
echo $value;
?>
This will work if you only have one "/".
$str = '2/5';
$newStr = explode('/', $str);
echo $newStr[0]/$newStr[1];
Edited.
Not elegant because eval
is used, but working.
$operation = '1/2';
function stringCalc($operation){
return eval('return '.$operation.';');
}
$value = stringCalc($operation);