PHP:intval() 相当于数字 >= 2147483647

发布于 2024-07-23 22:16:02 字数 825 浏览 7 评论 0原文

在 PHP 5 中,每当我获取数字作为输入时,我都会使用 intval() 。 这样,我想确保我没有得到字符串或浮点数。 我输入的数字应该都是整数。 但是当我得到数字 >= 2147483647 时,就超出了有符号整数限制。

我该怎么做才能让 intval() 等效于所有大小的数字?

这就是我想要的:

<?php
$inputNumber = 3147483647.37;
$intNumber = intvalEquivalent($inputNumber);
echo $intNumber; // output: 3147483647
?>

提前非常感谢!

编辑:根据一些答案,我尝试编写等效的函数。 但它还不能完全像 intval() 那样工作。 我该如何改进它? 这有什么问题吗?

function intval2($text) {
    $text = trim($text);
    $result = ctype_digit($text);
    if ($result == TRUE) {
        return $text;
    }
    else {
        $newText = sprintf('%.0f', $text);
        $result = ctype_digit($newText);
        if ($result == TRUE) {
            return $newText;
        }
        else {
             return 0;
        }
    }
}

In PHP 5, I use intval() whenever I get numbers as an input. This way, I want to ensure that I get no strings or floating numbers. My input numbers should all be in whole numbers. But when I get numbers >= 2147483647, the signed integer limit is crossed.

What can I do to have an intval() equivalent for numbers in all sizes?

Here's what I want to have:

<?php
$inputNumber = 3147483647.37;
$intNumber = intvalEquivalent($inputNumber);
echo $intNumber; // output: 3147483647
?>

Thank you very much in advance!

Edit: Based on some answers, I've tried to code an equivalent function. But it doesn't work exactly as intval() does yet. How can I improve it? What is wrong with it?

function intval2($text) {
    $text = trim($text);
    $result = ctype_digit($text);
    if ($result == TRUE) {
        return $text;
    }
    else {
        $newText = sprintf('%.0f', $text);
        $result = ctype_digit($newText);
        if ($result == TRUE) {
            return $newText;
        }
        else {
             return 0;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烟酒忠诚 2024-07-30 22:16:05
<?php
$a = 453453445435.4;

echo $bigint = floor($a);
<?php
$a = 453453445435.4;

echo $bigint = floor($a);
星星的軌跡 2024-07-30 22:16:04

如果要处理任意精度的数字,则需要使用 gmp< /a> 扩展名。 函数 gmp_init()例如,将字符串转换为 gmp 资源对象。 缺点是您必须使用该扩展的其他函数来进一步处理该对象。 使用 gmp_strval()< 将其转换回字符串例如,/code>

$gmpObject = gmp_init($string, 10);
if ($gmpObject === FALSE) {
    # The string was not a valid number,
    # handle this case here
}
echo gmp_strval($gmpObject);

如果您不打算对该值执行任何操作,您可能只想验证该字符串是否表示有效数字并使用该字符串本身。 这可以使用正则表达式来完成:

$containsInt = preg_match('/^\d+$/', $string);
# Or for floating point numbers:
$containsFloat = preg_match('/^\d+(.\d+)?$/', $string);
echo $string;

另一种选择是使用 is_numeric()。 但该函数的转换次数超出了您的预期。 引用该函数的文档:

... +0123.45e6 是一个有效的数值...

If you want to handle arbitrary-precision numbers, you will need to use the gmp extension. The function gmp_init(), for example, converts a string to a gmp resource object. the drawback is that you must use other functions of that extension to further process that object. Converting it back to a string is done using gmp_strval(), for example.

$gmpObject = gmp_init($string, 10);
if ($gmpObject === FALSE) {
    # The string was not a valid number,
    # handle this case here
}
echo gmp_strval($gmpObject);

You might want to only verify that the string is representing a valid number and use the string itself, if you do not intend to do any operations on the value. That can be done using a regular expression:

$containsInt = preg_match('/^\d+$/', $string);
# Or for floating point numbers:
$containsFloat = preg_match('/^\d+(.\d+)?$/', $string);
echo $string;

Another option is to use is_numeric(). But that function does more conversion than you might like. Quoting from docs of that function:

... +0123.45e6 is a valid numeric value ...

夏日落 2024-07-30 22:16:04

除了 number_format 之外,您还可以使用 sprintf:

sprintf("%.0f", 3147483647.37) // 3147483647

但是,这两种解决方案都会遇到浮点数溢出的问题,例如:

sprintf("%.0f", 314734534534533454346483647.37) // 314734534534533440685998080

In addition to number_format, you can use sprintf:

sprintf("%.0f", 3147483647.37) // 3147483647

However, both solutions suffer from float overflow, for example:

sprintf("%.0f", 314734534534533454346483647.37) // 314734534534533440685998080
゛时过境迁 2024-07-30 22:16:04

使用 number_format($inputNumber, 0, '', '')

或者如果您只想检查它是否是整数,则使用 ctype_digit($inputNumber)

Don'不要使用建议的 is_numeric,因为浮点数也是数字。 例如 "+0123.45e6"is_numeric 接受

Either use number_format($inputNumber, 0, '', '')

Or if you only want to check if its a whole number then use ctype_digit($inputNumber)

Don't use the proposed is_numeric, as also floats are numeric. e.g. "+0123.45e6" gets accepted by is_numeric

苍风燃霜 2024-07-30 22:16:04

您还可以使用正则表达式删除初始数字部分之后的所有内容:

<?php
$inputNumber = "3147483647.37";
$intNumber = preg_replace('/^([0-9]*).*$/', "\\1", $inputNumber);
echo $intNumber; // output: 3147483647
?>

you can also use regular expressions to remove everything after the intial numeric parts:

<?php
$inputNumber = "3147483647.37";
$intNumber = preg_replace('/^([0-9]*).*$/', "\\1", $inputNumber);
echo $intNumber; // output: 3147483647
?>
波浪屿的海角声 2024-07-30 22:16:03

尝试这个函数,它会像 intval 一样正确删除任何小数并删除任何非数字字符。

<?php
function bigintval($value) {
  $value = trim($value);
  if (ctype_digit($value)) {
    return $value;
  }
  $value = preg_replace("/[^0-9](.*)$/", '', $value);
  if (ctype_digit($value)) {
    return $value;
  }
  return 0;
}

// SOME TESTING
echo '"3147483647.37" : '.bigintval("3147483647.37")."<br />";
echo '"3498773982793749879873429874.30872974" : '.bigintval("3498773982793749879873429874.30872974")."<br />";
echo '"hi mom!" : '.bigintval("hi mom!")."<br />";
echo '"+0123.45e6" : '.bigintval("+0123.45e6")."<br />";
?>

这是生成的输出:

"3147483647.37" : 3147483647
"3498773982793749879873429874.30872974" : 3498773982793749879873429874
"hi mom!" : 0
"+0123.45e6" : 0

希望有帮助!

Try this function, it will properly remove any decimal as intval does and remove any non-numeric characters.

<?php
function bigintval($value) {
  $value = trim($value);
  if (ctype_digit($value)) {
    return $value;
  }
  $value = preg_replace("/[^0-9](.*)$/", '', $value);
  if (ctype_digit($value)) {
    return $value;
  }
  return 0;
}

// SOME TESTING
echo '"3147483647.37" : '.bigintval("3147483647.37")."<br />";
echo '"3498773982793749879873429874.30872974" : '.bigintval("3498773982793749879873429874.30872974")."<br />";
echo '"hi mom!" : '.bigintval("hi mom!")."<br />";
echo '"+0123.45e6" : '.bigintval("+0123.45e6")."<br />";
?>

Here is the produced output:

"3147483647.37" : 3147483647
"3498773982793749879873429874.30872974" : 3498773982793749879873429874
"hi mom!" : 0
"+0123.45e6" : 0

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文