【问题标题】:Getting percentage of conversions in Smarty template?在 Smarty 模板中获取转化百分比?
【发布时间】:2014-01-18 16:15:07
【问题描述】:

我的 smarty 模板中有两个不同的变量:

{$visitors} - 显示链接的点击次数。 {$numitems} - 显示来自点击的注册数量。

我需要以百分比形式获得转化率,数学上为{$numitems}/{$visitors}*100

有谁知道如何在 smarty 模板系统中执行此操作 - 并将其限制为小数点后 2 个数字 - 例如 3.81

【问题讨论】:

  • 首先,您遇到的其他问题是否通过 if/else 问题得到解决?

标签: php math smarty


【解决方案1】:

你可以试试这个吗?

{($numitems/$visitors*100)|string_format:"%.2f"}

也许您必须分两步完成:

{$x=$numitems/$visitors*100}
{$x|string_format:"%.2f"}

【讨论】:

    【解决方案2】:

    那你应该看看数学

    http://www.smarty.net/docsv2/en/language.function.math.tpl

    因为我不知道您使用的是版本 2 还是版本 3,因为您从未回答过这个问题。你可以使用数学,它可以在任何一个中工作。

    例如 你可以这样做。

    {math assign="$percentage" equation="(x / y) * 100" x=$numitems y=$visitors format="%.2f"}
    

    然后您可以使用{$percentage} 显示金额或执行任何操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 2021-11-28
      • 2012-12-12
      • 1970-01-01
      相关资源
      最近更新 更多