Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php smarty将字符串转换为数字_Php_Smarty_Smarty2 - Fatal编程技术网

Php smarty将字符串转换为数字

Php smarty将字符串转换为数字,php,smarty,smarty2,Php,Smarty,Smarty2,我需要做偏差: {math equation="x/y" x=$x y=$y} // $x = '2', $y = '3' 如何在Smarty中准确地将字符串转换为数字,因为我无法访问CMS的专有代码? 塔克斯 UPD:Smarty版本:2.6.18如果已将其分配给变量,例如$var,则可以如下设置变量类型: {$converted = settype ($var, 'integer')} 您不必使用$converted值,但如果不分配它,bool将显示在页面中 或者你可以试试 {$vari

我需要做偏差:

{math equation="x/y" x=$x y=$y} // $x = '2', $y = '3'
如何在Smarty中准确地将字符串转换为数字,因为我无法访问CMS的专有代码? 塔克斯


UPD:Smarty版本:2.6.18

如果已将其分配给变量,例如$var,则可以如下设置变量类型:

{$converted = settype ($var, 'integer')}
您不必使用$converted值,但如果不分配它,bool将显示在页面中

或者你可以试试

{$variable|intval}

你可以试试这个
{$variable | intval}

还有
((int)$variable)

例如:

$x_new = (int) $x;
$y_new = (int) $y;
就你而言:

{math equation="x/y" x=(int)$x y=(int)$y}

它在以下方面无需修改即可工作:

PHP:

Smarty:

{math equation="x/y" x=$x y=$y}
显示的结果是:
0.666 7

它在最新可用的Smarty版本2.6.28中进行了测试

<> P>所以你应该考虑升级到最新的SMATY(当然,你必须备份所有的东西以防出现任何问题),因为旧版本中有很多bug。
在你的问题中,你还没有写什么结果显示了smarty,smarty中显示的变量x和y值是什么?

int-typecast-in-equation是允许的(参见这里:),所以你可以简单地这样做
{math-equation=“(int)x/(int)y”x=$x y=$y}
@sofl。最短的方法是将其乘以
1
,因为乘以
1
的数字始终保持不变,
*
运算符仅用于数字,因此尚未转换为数字的变量将被转换<代码>{math equation=“x/y”x=$x*1 y=$y*1}
{math equation="x/y" x=$x y=$y}