gpt4 book ai didi

javascript - 如何知道一个数达到限制整数

转载 作者:行者123 更新时间:2023-12-01 00:48:21 25 4
gpt4 key购买 nike

Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

我遇到的问题是如何知道一个数字大于整数范围。 (抱歉我的英语不好)

最佳答案

您可以从 Signed 32-bit integers 获取值并检查范围。

function getValue(v) {
return v < -2147483648 || v > 2147483647
? 0
: v;
}

console.log(getValue(1));
console.log(getValue(1e20));

关于javascript - 如何知道一个数达到限制整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57198243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com