2011-02-04 35 views
7

所有小數得到這個從我的數據庫:刪除在PHP

252.587254564

嗯,我想刪除.587254564並保持252,我該怎麼辦呢?

我應該使用什麼功能,你能告訴我一個例子嗎?

問候

回答

11

你可以在PHP中做到這一點:

round($val, 0); 

或在你的MySQL聲明:

select round(foo_value, 0) value from foo 
1

可以使用echo (int) 252.587254564;

2

正如Tricker提到你可以圓價值下降,或者你可以把它像這樣int:

$variable = 252.587254564; // this is of type double 
$variable = (int)$variable; // this will cast the type from double to int causing it to strip the floating point. 
2

您可以只將其轉換爲int

$new = (int)$old; 
5

你可以做一個簡單的轉換爲int

$var = 252.587254564; 
$var = (int)$var; // 252 
0

在MySQL中,你可以使用:

select floor(field) 

或PHP,你可以使用:

floor($value); 
4

在PHP中,你可以使用:

$值=地板($值);

floor如有必要,通過舍入值返回下一個最小的整數值。

如果你想圓了這將是

$值=小區($值);

ceil如有必要,通過舍入值返回下一個最高整數值。

0

而且還有一個不太明智的方法:

strtok($value, "."); 

第一部分的這削減,直到遇到一個點。結果將是一個字符串,而不是一個PHP整數。雖然它不會影響使用結果,但它不是最好的選擇。