Jump to content

Recommended Posts

Posted

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?

Posted

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.)

Posted

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.

Posted

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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...