Smarty:从字符串/浮点值的末尾修剪零

时间:2014-10-25 18:16:28

标签: php templates smarty trim modifier

需要格式化值,以这种方式从末尾剪切零:

3     => 3 
3.    => 3 
3.0   => 3 
3.00  => 3 
3.000 => 3 
3.009 => 3.009 
3.003 => 3.003
3.01  => 3.01 
3.10  => 3.1 
3.16  => 3.16 

找到这些Smarty修改器插件来完成这项工作{$ var | zero_cut:4}:

Source: http://www.smarty.net/forums/viewtopic.php?p=17482
vl@vl plugins $ cat modifier.zero_cut.php 
<?php 

/** 
   * Smarty insignificant zero cutter modifier plugin 
   * 
   * Type:     modifier<br> 
   * Name:     zero_cut<br> 
   * Purpose:  Format string, representing float value with given 
   *           number of digits after decimal point with deletion 
   *           of insignificant zeros. 
   * 
   * Example:  {$var|zero_cut:2} , where 2 is number of significant 
   *                               digits after decimal point to show 
   *           if number is 0(by default), function returns 
   *           sprintf("%.0f",$str) 
   *              
   *           input string must be separated with '.' symbol 
   * 
   * Date:     January 29,2005 
   * @author   `VL <vl409@yandex.ru> 
   * @version  1.0 
   * @param string 
   * @param integer 
   * @return string 
   * 
   * Example output: 
   * 
   * 3     => 3 
   * 3.    => 3 
   * 3.0   => 3 
   * 3.00  => 3 
   * 3.000 => 3 
   * 3.009 => 3.01 
   * 3.003 => 3 
   * 3.01  => 3.01 
   * 3.10  => 3.1 
   * 3.16  => 3.16 
   * 
   */ 

function smarty_modifier_zero_cut($str,$digits=0) 
{ 
        # format value with given number of digits after decimal point 
        $value=sprintf("%.${digits}f",$str); 

        if(0==$digits) 
                return $value; 

        # break it in 2 parts 
        list($left,$right)=explode (".",$value); 

        # now we move the string, starting from the end 
        # and counting how many insignificant zeros exists 

        $len=strlen($right); # got length 
        $k=0; # how many symbols to skip,starting from end of string 
 { 
                # found insignificant zero, increase counter 
                if('0'==$right{$i}) 
                        $k++; 
                else 
                        break; # found insignificant digit, stop moving 
        } 

        # drop counted number of symbols at the end of string 
        $right=substr($right,0,$len-$k); 

        # if right part is not empty, add decimal point symbol 
        if(""!=$right) 
                $right=".$right"; 

        # return whole value 
        return $left.$right; 
} 

?> 

        for($i=$len-1;$i>=0;$i--) 
 { 
                # found insignificant zero, increase counter 
                if('0'==$right{$i}) 
                        $k++; 
                else 
                        break; # found insignificant digit, stop moving 
        } 

        # drop counted number of symbols at the end of string 
        $right=substr($right,0,$len-$k); 

        # if right part is not empty, add decimal point symbol 
        if(""!=$right) 
                $right=".$right"; 

        # return whole value 
        return $left.$right; 
} 

?> 

没关系。但是我想问一下 - 还有其他一些标准方法吗?

1 个答案:

答案 0 :(得分:6)

您可以使用rtrim函数

为这些数字做

PHP数据:

$data = ['3', '3.','3.0','3.00','3.000','3.009','3.003','3.01','3.10','3.16'];

$smarty->assign('values',$data);

Smarty档案:

{foreach $values as $val}
    {$val} => {$val|rtrim:'0'|rtrim:'.'}<br />
{/foreach}

输出:

3 => 3
3. => 3
3.0 => 3
3.00 => 3
3.000 => 3
3.009 => 3.009
3.003 => 3.003
3.01 => 3.01
3.10 => 3.1
3.16 => 3.16

但显然它在所有情况下都不会起作用。例如,对于30,它也会将其修剪为3,因此您应该稍微修改它以检测参数中的.

{foreach $values as $val}
    {$val} =>
    {if $val|strpos:'.' eq true}
        {$val|rtrim:'0'|rtrim:'.'}<br />
     {else}
        {$val}<br />
    {/if}
{/foreach}