删除PHP中的所有小数

The*_*hew 10 php database

从我的数据库中获取:

252.587254564

好吧,我想删除.587254564并保留252,我该怎么办?

我应该使用什么功能,你能告诉我一个例子吗?

问候

Yor*_*gen 19

你可以用PHP做到这一点:

round($val, 0);
Run Code Online (Sandbox Code Playgroud)

或者在你的MYSQL语句中:

select round(foo_value, 0) value from foo
Run Code Online (Sandbox Code Playgroud)

  • 我认为这不是正确的答案。提问者并不打算进行四舍五入。询问者有 252.587254564 并希望删除小数并留下 252。round($val, 0) 将得到 253(向上舍入)。具体问题的正确答案不应向上舍入,而应删除小数或始终向下舍入。我会使用 intval($val) (3认同)

Mur*_*los 8

你可以做一个简单的演员int.

$var = 252.587254564;
$var = (int)$var; // 252
Run Code Online (Sandbox Code Playgroud)


lud*_*ign 6

正如 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.
Run Code Online (Sandbox Code Playgroud)


Mar*_*oVW 5

您可以将其转换为int

$new = (int)$old;
Run Code Online (Sandbox Code Playgroud)


Joe*_*eyH 5

在PHP中,您将使用:

$value = floor($value);
Run Code Online (Sandbox Code Playgroud)

floor:如果需要,通过舍入该值来返回下一个最小整数值。

如果您想四舍五入,那就是:

$value = ceil($value);
Run Code Online (Sandbox Code Playgroud)

ceil:如果需要,将值四舍五入来返回下一个最大的整数值。