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?