smarty中math函数的用法

在查看项目代码时,发现smart代码中有这样写。

{if $auctionInfo.printingNum}
    印数:{math equation="cid/1000" cid=$auctionInfo.printingNum format="%d"}千册<br />
{/if}

意思是:把$auctionInfo.printingNum 的值赋值给cid,cid/1000,后面format是格式,数值型,如果要保留一位  format = "%.1f" ,保留两位 format = "%.2f"

以下是官网给举例子:

Example 8.24. {math}

Example a:

   {* $height=4, $width=5 *}

   {math equation="x + y" x=$height y=$width}

The above example will output:

   9

Example b:

   {* $row_height = 10, $row_width = 20, #col_div# = 2, assigned in template *}

   {math equation="height * width / division"
   height=$row_height
   width=$row_width
   division=#col_div#}

The above example will output:

   100

Example c:

   {* you can use parenthesis *}

   {math equation="(( x + y ) / z )" x=2 y=10 z=2}

The above example will output:

   6

Example d:

   {* you can supply a format parameter in sprintf format *}

   {math equation="x + y" x=4.4444 y=5.0000 format="%.2f"}

The above example will output:

   9.44

  url:http://www.smarty.net/docsv2/en/language.function.math.tpl
原文地址:https://www.cnblogs.com/wangkongming/p/3671295.html