dougutuo9879 2014-10-25 18:16
浏览 51
已采纳

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

Need to format value, cutting zeros from the end, in this way:

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 

Found these Smarty modifier plugin that will do the job {$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; 
} 

?> 

It's ok., but I want to ask - is there some other, standard method to do this?

  • 写回答

1条回答 默认 最新

  • dtng5978 2014-10-26 09:11
    关注

    You could do for such numbers, using rtrim function:

    PHP data:

    $data = ['3', '3.','3.0','3.00','3.000','3.009','3.003','3.01','3.10','3.16'];
    
    $smarty->assign('values',$data);
    

    Smarty file:

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

    Output:

    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
    

    but obviously it won't work in all cases. For example for 30 it will also trim it to 3 so you should modify it a bit to detect . in argument:

    {foreach $values as $val}
        {$val} =>
        {if $val|strpos:'.' eq true}
            {$val|rtrim:'0'|rtrim:'.'}<br />
         {else}
            {$val}<br />
        {/if}
    {/foreach}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!