PHP關於數據的舍入歸類:round、ceil、floor、number_format

下面是關於數字轉化的幾種方法:

round -- 對浮點數進行四捨五入

例子:

<?php
echo round(3.4);         // 3


echo round(3.6);         // 4

echo round(3.60);      
// 4

echo round(1.955832);  
// 1.96

echo round(1241757, -3); // 1242000


echo round(5.0552);    // 5.06

?>

ceil -- 進一法取整

例子:

<?php
echo ceil(4.3);    
// 5

echo ceil(9.999);  
// 10

?>


floor -- 捨去法取整,跟ceil剛剛相反

例子:

<?php
echo floor(4.3);   
// 4

echo floor(9.999); 
// 9
?>

number_format -- 格式化數字爲千分位


<?php php

$number = 1234; spa

// english notation (default)
$english_format_number = number_format($number);
echo "First:".$english_format_number."<br />"; orm

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
echo "Second:".$nombre_format_francais."<br />"; it

// French notation
$number = 1234.564;
$nombre_format_francais = number_format($number, 2, ',', ' ');
echo "Third:".$nombre_format_francais."<br />";

$number = 1234.5678; io

// english notation without thousands seperator
$english_format_number = number_format($number, 2, '.', '');
echo "Forth:".$english_format_number."<br />"; form

?> 方法

顯示結果最終以下: First:1,234 Second:1234,00 Third:1234,56 Forth:1234.57
相關文章
相關標籤/搜索