开发者

How to convert a numeric number into string?

开发者 https://www.devze.com 2023-01-05 03:17 出处:网络
I need to test if two variables are equals. But one is in string format and the other is a number. So, I try to convert the 开发者_高级运维variable in text, but without success.

I need to test if two variables are equals. But one is in string format and the other is a number.

So, I try to convert the 开发者_高级运维variable in text, but without success.

Have you an idea ?


Try using {$number|string_format:"%.2f"} to convert both numbers to strings and compare them. or {$number|string_format:"%d"} if it are integer, instead of float numbers.

I don't see a way in smarty to explicitly cast a sting to an integer, so you should convert both to a string.

If it doesn't work, please post your code. Maybe the issue lies in a different line..


I had the same problem due to the php auto converting array keys (string) to integers based on the fact that they look like integers (this is actually a documented "feature"!) and then messing up my comparison in Smarty. The straight forward and quite an awful way to do it is:

{php}$this->assign('STR', strval($this->get_template_vars('NUM')));{/php}

If you need to use it more often in your code, consider writing a custom Smarty {strval}.


You can use settype to convert the integer to a string, then compare them...

$var1 = 10;
$var2 = "10";
{$converted = settype($var2, 'integer')}

OR

{$converted = settype($var1, 'string')}

That will give you vars of the same type. The only reason to have the $converted variable is because the bool returned will appear on your page if it isn't assigned.

0

精彩评论

暂无评论...
验证码 换一张
取 消