欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php float不四舍五入截取浮點(diǎn)型字符串方法總結(jié)

 更新時間:2013年10月28日 10:02:04   作者:  
在php中截取浮點(diǎn)型大致有以下幾種方法。需要的朋友可以過來參考下,希望對大家有所幫助

php中截取浮點(diǎn)型大致有下面幾種方法:

1、 float round ( float $val [, int $precision ] ) 返回將 val 根據(jù)指定精度 precision (十進(jìn)制小數(shù)點(diǎn)后數(shù)字的數(shù)目)進(jìn)行四舍五入的結(jié)果。precision 也可以是負(fù)數(shù)或零(默認(rèn)值)。

    echo round(4.3)  //4

2、 string sprintf ( string $format [, mixed $args [, mixed $... ]] ) 返回格式化數(shù)據(jù)的字符串

復(fù)制代碼 代碼如下:

$a=12.338938438;
echo sprintf("%.5f",$a) //結(jié)果:12.33894 

$a=12.3312356;
echo sprintf("%.5f",$a);//12.33124
echo sprintf("%f",$a);//331236  默認(rèn)小數(shù)點(diǎn)后6位

3、 string number_format ( float $number , int $decimals , string $dec_point , string $thousands_sep )
復(fù)制代碼 代碼如下:

$number = 1234.5678;

$english_format_number = number_format($number, 2, '.', '');
echo  $english_format_number ; // 1234.57

以上這些都自動做了四舍五入,有時候需求不需要四舍五入呢,怎么辦,沒有想到好辦法,誰知道可以告訴一聲。

自己寫了個麻煩點(diǎn)的函數(shù),記錄下

復(fù)制代碼 代碼如下:

function getFloatValue($f,$len)
{
  $tmpInt=intval($f);

  $tmpDecimal=$f-$tmpInt;
  $str="$tmpDecimal";
  $subStr=strstr($str,'.');
  if(strlen($subStr)<$len+1)
 {
  $repeatCount=$len+1-strlen($subStr);
  $str=$str."".str_repeat("0",$repeatCount);

 }

  return    $tmpInt."".substr($str,1,1+$len);

}
echo getFloatValue(12.99,4) //12.9900
echo getFloatValue(12.9232555553239,4) //12.9232

相關(guān)文章

最新評論