Re: Converting a String to Int

From: Date: Wed, 09 Apr 2003 12:04:35 +0000
Subject: Re: Converting a String to Int
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-143051@lists.php.net to get a copy of this message
On Wednesday 09 April 2003 17:08, Burhan Khalid wrote: > On Wed, 2003-04-09 at 01:51, Jason Wong wrote: > > On Wednesday 09 April 2003 02:21, Poon, Kelvin (Infomart) wrote: > > > > [snip] > > > > > I get a number that is wrong because > > > $num isn't an integer, I am guessing it has to do with the comma > > > (3,469) in $num. Can I convert $num=3,469 into an int? If not, can I > > > maybe take out the comma and then convert it to int? Please help > > > > str_replace() the comma with nothing. > > Sorry, but this won't work. It will return a string. PHP automatically converts strings into numbers where appropriate. The string '3,469' gets converted to 3. > Instead, you should use intval() -- which will return the desired > result. > > $val = intval($num); > > http://www.php.net/intval Try this: $doo = '3,469'; $dah = str_replace(',', '', $doo); echo $dah + 1; echo "<br>"; $num = '3,469'; $val = intval($num); echo $val; -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * ------------------------------------------ Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general ------------------------------------------ /* The only difference between the saint and the sinner is that every saint has a past and every sinner has a future. -- Oscar Wilde */

« previous php.general (#143051) next »