c# - 无法获得 Math.Abs​​(-2147483648) 的值(value)

标签 c#

这是我的代码。

string strTemp = "";
for (int i = input1; i <= input2; i++)
{
    strTemp = Math.Abs(i).ToString();
    //other stuff here
}

当 input1 的值为 -2147483648 时出现错误

Negating the minimum value of a twos complement number is invalid.

我有点明白这是由于溢出异常造成的。 但是我该如何克服呢?请提出任何建议。

最佳答案

+2147483648 大于 int 的最大值,即 231 - 1。
相反,您可以使用 long;它的最大值是 263 - 1。

如果这对你来说还不够;使用 BigInteger

关于c# - 无法获得 Math.Abs​​(-2147483648) 的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16131544/

相关文章:

c# - SignalR 和数据库上下文处理

c# - 如何使用 Async/Await 进行进度报告

c# - 嵌套Task.WhenAll有什么限制吗?

javascript - 将唯一的时间跨度插入表中

c# - Linq Where 列表包含另一个列表中的项目

c# - Resharper 自定义模式更改方法名称

c# - 如何从 C# 应用程序发送签名的电子邮件?

c# - WPF Prism 找不到 GetModuleCatalog(),找不到要重写的方法

c# - 我可以在业务层使用 TransactionScope 而不是在数据提供程序中使用 SQLiteTransaction 吗?

c# - IList<mutable_struct> 与 mutable_struct[]