php中截取浮點型大致有下面幾種方法:
1、 float round ( float $val [, int $precision ] ) 返回將 val 根據指定精度 precision (十進制小數點后數字的數目)進行四舍五入的結果。precision 也可以是負數或零(默認值)。
echo round(4.3) //4
2、 string sprintf ( string $format [, mixed $args [, mixed $... ]] ) 返回格式化數據的字符串
復制代碼 代碼如下:
$a=12.338938438; 
echo sprintf("%.5f",$a) //結果:12.33894  
$a=12.3312356; 
echo sprintf("%.5f",$a);//12.33124 
echo sprintf("%f",$a);//331236  默認小數點后6位 
復制代碼 代碼如下:
$number = 1234.5678; 
$english_format_number = number_format($number, 2, '.', ''); 
echo  $english_format_number ; // 1234.57 
自己寫了個麻煩點的函數,記錄下
復制代碼 代碼如下:
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 
新聞熱點
疑難解答