jstern Posted May 11, 2010 Report Share Posted May 11, 2010 Hopefully a quick Q I have a string with the value "4,120.00" I'd like to convert this to a int or a float. (Im hoping without having to strip the string of its comma myself) the value is set using: $this->_value = round($value, 2); when I use $this->_value = ((float)$value); it just returns "4" (becuase of the comma). Same thing with (int). Anyone know a quick solution? Quote Link to comment Share on other sites More sharing options...
falkencreative Posted May 11, 2010 Report Share Posted May 11, 2010 Perhaps use str_replace() (http://php.net/manual/en/function.str-replace.php) to remove the comma from the string, and then convert it? Quote Link to comment Share on other sites More sharing options...
jstern Posted May 11, 2010 Author Report Share Posted May 11, 2010 yessum if (is_string($value)) { $value = (str_replace(",","",$value)); $this->_value = round($value, 2); } seems to be the fix i needed. I dont know why i was looking for something easier than this, this was pretty dang simple lol. (*is_string is only important due to various types being passed through my method.) Quote Link to comment Share on other sites More sharing options...
is_numeric Posted May 15, 2010 Report Share Posted May 15, 2010 cant you just cast the type to what you want $newVar = (int)$var; Quote Link to comment Share on other sites More sharing options...
falkencreative Posted May 15, 2010 Report Share Posted May 15, 2010 cant you just cast the type to what you want $newVar = (int)$var; Unfortunately not. It gets confused when it hits the comma, so "1,000" will turn into "1". See the first post in this thread - JStern mentioned that. Quote Link to comment Share on other sites More sharing options...
krillz Posted May 15, 2010 Report Share Posted May 15, 2010 cant you just cast the type to what you want $newVar = (int)$var; implicit transformation just cuts off the decimals it does not conduct a mathematical calculation to round the number to the nearest integer following the mathematical rules. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.