php 如何删除PHP中点之后的所有数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2841109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:51:33  来源:igfitidea点击:

How to delete all numbers after point in PHP

php

提问by lovespring

example: 1.123 =>1 1.999 => 1

例如:1.123 => 1 1.999 => 1

thanks.

谢谢。

回答by selfawaresoup

$y = 1.235251;
$x = (int)$y;
echo $x; //will echo "1"

Edit:Using the explicit cast to (int) is the most efficient way to to this AFAIK. Also casting to (int) will cut off the digits after the "." if the number is negative instead of rounding to the next lower negative number:

编辑:使用显式强制转换为 (int) 是此 AFAIK 的最有效方法。也投射到 (int) 将切断“。”之后的数字。如果数字是负数而不是四舍五入到下一个较低的负数:

echo (int)(-3.75); //echoes "-3";
echo floor(-3.75); //echoes "-4";

回答by LesterDove

floor() 

will round a number down to the nearest integer.

将数字向下舍入到最接近的整数。

EDIT: As pointed out by Mark below, this will only work for positive values, which is an important assumption. For negative values, you'd want to use ceil()-- but checking the sign of the input value would be cumbersome and you'd probably want to employ Mark's or TechnoP's (int) castidea instead. Hope that helps.

编辑:正如下面马克所指出的,这仅适用于正值,这是一个重要的假设。对于负值,您想使用ceil()-- 但检查输入值的符号会很麻烦,您可能想改用 Mark 或 TechnoP 的(int) cast想法。希望有帮助。

回答by Rafalages

You could use a bitwise operator.

您可以使用按位运算符。

Without:

没有:

echo 49 / 3;
>> 16.333333333333

With "| 0" bitwise:

使用“ | 0”按位:

echo 49 / 3 | 0;
>> 16

回答by nothrow

$y = 1.234;
list($y) = explode(".", "$y");

回答by Mark Byers

If your input can only be positive floats then as already mentioned floor works.

如果您的输入只能是正浮点数,那么正如已经提到的地板工程。

floor(1.2)

However if your integer could also be negative then floor may not give you what you want: it always rounds down even for negative numbers. Instead you can cast to int as another post mentioned. This will give you the correct result for both negative and positive numbers.

但是,如果您的整数也可能是负数,那么 floor 可能无法满足您的要求:即使对于负数,它也总是向下舍入。相反,您可以像提到的另一篇文章一样转换为 int 。这将为您提供负数和正数的正确结果。

(int)-1.2

回答by Shorabh

To remove all number after point use some php function

要删除点后的所有数字,请使用一些 php 函数

echo round(51.5); // Round the number, return 51.
echo floor(51.5); // Round down number, return 51.
echo ceil(51.3); // Round up number, return 52.