Hello:
Is it possible to convert 2 1/3 to its decimal number via php?
Hope someone can help me.
Thanks!
Hello:
Is it possible to convert 2 1/3 to its decimal number via php?
Hope someone can help me.
Thanks!
How do you enter 2 1/3?
yep it's possible. Even easy.
You can use regular expressions to ensure that your input string is in the form
<WholeNumber><Space><Fraction>
with the <Space><Fraction> bit optional.
Then you use explode, or regular expressions to separate out the WholeNumber and (optional) Fraction into two different variables.
Do some additional checks to make sure that WholeNumber is numeric etc.
If there is a Fraction, use regular expressions to ensure that it's in the format <Numberator><Slash><Denominator>.
Use regular expressions or explode to separate Numerator and Denominator into two different variables. Do some additional check to ensure that Numerator and Denominator are both numeric, that Denominator is non zero, etc.
Finally you end up with
$wholeNumber
$numerator
$denominator
And you can return the value of $wholeNumber + ($numerator / $denominator)