1
의 끝에서 트림 제로는 이런 방식으로, 끝에서 0을 절단 값을 포맷해야합니다스마티 : 문자열/부동 소수점 값
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
을 일을 할 것입니다 이러한 변경자 플러그인을 찾을 수 {$ var에 | zero_cut : 4} :
Source: http://www.smarty.net/forums/viewtopic.php?p=17482
[email protected] 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 <[email protected]>
* @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;
}
?>
괜찮습니다.하지만 다른 방법이 있습니까?
당신이 라운드 수정을 시도해 봤어 :
30
예를 들어 그것은 또한3
에 트림 것이다 그래서 당신은 그것을 인수.
을 감지하는 약간 수정해야합니까? http://stackoverflow.com/a/14744266/173934 – Cups라운드 라운드, 트림 0이 아닌 라운드 할 필요가 없습니다. – BlueMark