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}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀