RE: [PHP] convert string to number
| From: | Jay Blanchard | Date: | Mon, 17 Nov 2003 16:54:20 +0000 |
| Subject: | RE: [PHP] convert string to number | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-169977@lists.php.net to get a copy of this message | ||
[snip]
> > [snip]
> > what is the easiest way to convert a string like this "1,300.99" to
a
> > number?
> > [/snip]
>
> http://www.php.net/settype
That'll just result in the number 1, though, since the conversion to
"float"
will stop at the comma.
If you know that it's just going to be commas in there, a simple
str_replace(',','',$number) will get rid of the commas. Then PHP's type
juggling will take care of the rest for you and treat it like a FLOAT if
it
needs to be. Or you can then use settype() to ensure it's a float.
[/snip]
*oops* You are right John, I pulled the trigger too soon in myresponse.
The brain skipped the comma entirely.