【问题标题】:I am getting the error "Overflow." in Visual Basic 2010我收到错误“溢出”。在 Visual Basic 2010 中
【发布时间】:2015-06-26 08:58:33
【问题描述】:
Dim clicks As Integer
clicks = 0

If clicks >= 10000000000000000000 Then
    a19.ForeColor = Color.FromArgb(0, 153, 0)
End If

10000000000000000000 及以上会给出错误。 有没有办法拥有无限的大长度数字?

【问题讨论】:

  • 你有什么特别的理由需要使用这么大的数字吗?在这种情况下,这真的没有意义。 (点击)
  • 以每秒 100 次点击的速度,在 76 亿年之后将达到 10000000000000000000 次点击——超过宇宙年龄的一半!

标签: vb.net numbers integer runtime-error largenumber


【解决方案1】:

您可以使用 Long 而不是 Integer 来允许您最多达到 9 * 10E18(9 后跟 18 个零)。如果文字是长的,请在文字上使用 L 后缀

Dim bigNumber as Long = 5000000000000000000L

如果您想要任何大小的整数,请查看BigInteger Structure。例如(以下需要引用 System.Numerics DLL 以及声明 Imports System.Numerics

    Dim clicks As BigInteger = 0

    'Code that updates click goes here

    Dim limit As BigInteger = BigInteger.Parse("10000000000000000000")
    If clicks >= limit Then
        a19.ForeColor = Color.FromArgb(0, 153, 0)
    End If

您也可以使用 Double 来保存 1E308 这么大的数字,但它不能保存 308 位十进制数字,因此数字只能是近似值。

【讨论】:

  • 使用 BigInteger 会像:Dim clicks As New BigInteger 或 Dim clicks As BigInteger ?
  • 这是一个结构,所以Dim clicks As BigInteger。请参阅我链接到的文档。您需要添加对 System.Numerics 的引用。
  • 所以 ii 添加了它,但有部分下划线(错误) /跨度>
  • 我更新了我的答案以包含使用 BigInteger 的原始代码版本。
  • 我不相信您可以在 MySettings 中存储 BigInteger,这意味着您必须在保存之前将 BigInteger 转换为其他内容。一种可能性是使用 BigInteger 的 ToString 方法将其转换为字符串。当您想将 String 转换回 BigInteger 时,可以使用 BigInteger.Parse
【解决方案2】:

在计算机科学中,数字最常表示为integer。整数通常定义为 32 位有符号 整数,这意味着该值只能容纳从 -(2^31) 到 (2^31)-1 的值。要使用更大的数字,您可能需要检查其他整数类型。最常用的较大整数是64位整数,它可以容纳从-(2^63)到(2^63)-1的值。

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    相关资源
    最近更新 更多