Re: how to conver a string to float
| From: | Michal Migurski | Date: | Sun, 08 Feb 2004 04:50:45 +0000 |
| Subject: | Re: how to conver a string to float | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-177121@lists.php.net to get a copy of this message | ||
>> Is there a possibility to convert a string like "10*500" (parsed from a
>> XML-File) to a float? When i try to store this String in a float
>> variable it only coverts the "10" but nothing after the "*"-sign, but i
>> need the result of this expresison... Is there a function to calculate
>> such string expressions?
Another possibility is to pipe the expression to bc, which could do the
input check for you, and probably covers a lot of syntactic ground.
Slightly less resource-efficient, but you don't have to reinvent the
wheel (untested, unix only):
$result = shell_exec("echo '${expr}' | bc");
---------------------------------------------------------------------
michal migurski- contact info and pgp key:
sf/ca http://mike.teczno.com/contact.html