Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Modifier to cut insignificant zeros in value

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
vl
Smarty n00b


Joined: 15 Jul 2004
Posts: 2
Location: Moscow,Russia

PostPosted: Sat Jan 29, 2005 7:52 pm    Post subject: Modifier to cut insignificant zeros in value Reply with quote

working with MySQL and DECIMAL type, i found that it always stores values with zeroes after decimal point, even if they are insignificant: for example, 10 is stored as 10.00. To avoid this on output, i wrote this modifier.

(It seems to me,that such conversion was possible with printf(), but i`m not sure ;-))

Code:

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;
}

?>

Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP