php 删除PHP中的所有小数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4903171/
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
Removing all decimals in PHP
提问by Thew
get this from my database:
从我的数据库中获取:
252.587254564
252.587254564
Well i wanna remove the .587254564
and keep the 252
, how can i do that?
好吧,我想删除.587254564
并保留252
,我该怎么做?
What function should i use and can you show me an example?
我应该使用什么功能,你能举个例子吗?
Greetings
你好
回答by Yoram de Langen
You can do it in PHP:
你可以在 PHP 中做到:
round($val, 0);
or in your MYSQL statement:
或在您的 MYSQL 语句中:
select round(foo_value, 0) value from foo
回答by Murilo Vasconcelos
You can do a simply cast to int
.
您可以简单地强制转换为int
.
$var = 252.587254564;
$var = (int)$var; // 252
回答by JoeyH
In PHP you would use:
在 PHP 中,您将使用:
$value = floor($value);
floor
: Returns the next lowest integer value by rounding the value down if necessary.
floor
:如有必要,通过将值向下舍入来返回下一个最小的整数值。
If you wanted to round up it would be:
如果你想四舍五入,那就是:
$value = ceil($value);
ceil
: Returns the next highest integer value by rounding the value up if necessary.
ceil
:如有必要,通过向上舍入值来返回下一个最高整数值。
回答by ludesign
As Tricker mentioned you can round the value down or you can just cast it to int like so:
正如 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.
回答by MarioVW
You can just cast it to an int
:
您可以将其强制转换为int
:
$new = (int)$old;
回答by u476945
you can use echo (int) 252.587254564;
您可以使用 echo (int) 252.587254564;
回答by Nanhe Kumar
Before using above answer what is your exact requirement please see bellow example output.
在使用上述答案之前,您的确切要求是什么,请参阅下面的示例输出。
$val = 252.587254564;
echo (int)$val; //252
echo round($val, 0); //253
echo ceil($val); //253
$val = 1234567890123456789.512345;
echo (int)$val; //1234567890123456768
echo round($val, 0);//1.2345678901235E+18
echo ceil($val); //1.2345678901235E+18
$val = 123456789012345678912.512345;
echo (int)$val; //-5670419503621177344
echo round($val, 0);//1.2345678901235E+20
echo ceil($val); //1.2345678901235E+20
回答by Tinh Dang
Convert the float number to string, and use intval to convert it to integer will give you 1990
将浮点数转换为字符串,并使用 intval 将其转换为整数将为您提供 1990
intval(("19.90"*100).'')
回答by mario
And there is also a not quite advisable method:
还有一个不太可取的方法:
strtok($value, ".");
This cuts of the first part until it encounters a dot. The result will be a string, not a PHP integer. While it doesn't affect using the result much, it's not the best option.
这将切割第一部分,直到遇到一个点。结果将是一个字符串,而不是一个 PHP 整数。虽然它不会对使用结果产生太大影响,但它不是最佳选择。
回答by Brian Fisher
In MySQL you can use:
在 MySQL 中,您可以使用:
select floor(field)
or in PHP you can use:
或者在 PHP 中你可以使用:
floor($value);