php smarty 将字符串转换为数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23695147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:53:05  来源:igfitidea点击:

smarty convert string to number

phpsmartysmarty2

提问by Sergey Kudryashov

I need to do deviding:

我需要做划分:

{math equation="x/y" x=$x y=$y} // $x = '2', $y = '3'

How to convert strings to numbers exactly in Smarty, coz I don't have access to proprietary code of CMS? Thanx!

如何在 Smarty 中准确地将字符串转换为数字,因为我无权访问 CMS 的专有代码?谢谢!

UPD: Smarty version: 2.6.18

UPD:Smarty 版本:2.6.18

回答by Guns

If it's already assigned to a variable, say $var, you can set the type of a variable like this:

如果它已经分配给一个变量,比如 $var,你可以像这样设置变量的类型:

{$converted = settype ($var, 'integer')}

You don't have to use the $converted value, but if you don't assign it, the bool will show up in your page.

您不必使用 $converted 值,但如果您不指定它,bool 将显示在您的页面中。

or you could try

或者你可以试试

{$variable|intval}

回答by Manwal

You can try this {$variable|intval}

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

and also ((int)$variable)

并且 ((int)$variable)

For Instance:

例如:

$x_new = (int) $x;
$y_new = (int) $y;

In your case:

在你的情况下:

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

回答by Marcin Nabia?ek

It works without modifications in:

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

PHP:

PHP:

$x = '2' ;
$y = '3' ;

$smarty->assign('x',$x);
$smarty->assign('y',$y);

$smarty->display('index.tpl');

Smarty:

聪明:

{math equation="x/y" x=$x y=$y}

Displayed result is: 0.66666666666667

显示结果为: 0.66666666666667

It was tested in the last available Smarty version 2.6.28

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

So you should consider trying upgrading to the newest Smarty (of course you have to backup everything in case it will cause any problems) because there were many bugs in older versions.

所以你应该考虑尝试升级到最新的Smarty(当然你必须备份所有东西,以防它引起任何问题),因为旧版本有很多错误。

You also in your question haven't written what results shows you smarty and what are variables x and y values displayed in Smarty/

您在问题中还没有写出哪些结果显示您很聪明,以及 Smarty/ 中显示的变量 x 和 y 值是什么