Php 如何删除点后的任何数字

Php 如何删除点后的任何数字,php,numbers,Php,Numbers,我想删除点后的任何数字。范例 $input = '33.892'; $input = '15.274856'; $input = '-3.14'; $input = '5.055'; 输出应为33、15、3和5。让我知道。您可能需要使用或使用适当的修改器。您可以通过以下方式实现: $input = str_replace('-', '', strstr($input, '.', true)); 请注意,您需要安装至少5.3.0版的PHP才能执行此操作。只需将该值解析为int: $input

我想删除点后的任何数字。范例

$input = '33.892';
$input = '15.274856';
$input = '-3.14';
$input = '5.055';

输出应为
33
15
3
5
。让我知道。

您可能需要使用或使用适当的修改器。

您可以通过以下方式实现:

$input = str_replace('-', '', strstr($input, '.', true));

请注意,您需要安装至少5.3.0版的PHP才能执行此操作。

只需将该值解析为int:

$input   = '33.892';
$input2  = '15.274856';
$input3  = '-3.14';
$input4  = '5.055';

$output  = (int) $input;
$output2 = (int) $input2;
$output3 = abs( (int) $input3 );
$output4 = (int) $input4;
快速总结:

  • 如果要删除点后的数字-请使用
    (int)
  • 如果要删除负号,请使用
    abs()

    • 很明显,你既不想要地板,也不想要天花板,所以这里就是你想要的:

      $input = '33.892';
      $explode = explode('.',$input);
      $output = $explode[0];
      
      享受!:)

      就这样做:

      $yourNumber = number_format($input, 0, '.', '');
      

      虽然这是一个解决方案,但它可能不是最好的解决方案,因为它输出一个字符串。@Gunnar:为什么不呢?输入也是一个字符串。OP没有要求将输入转换为整数或其他什么。我没有将
      abs
      添加到输出和output2以显示差异。如果您跳过
      abs()
      ,您将得到负输出3。(+1)有趣的解决方案:)但如果参数为负数(“-3.14”),这不会消除整数部分的减号。Explode的“昂贵”。
      地板(abs($number))
      有什么问题?