正加法运算产生负数

时间:2016-08-07 20:52:40

标签: c# int32

我正在编写一个脚本,从csv文件中获取一系列数字并将它们合计。

我已将csv中的值提取到0中,然后循环将其添加到一起。 这些数字是当天每分钟的毫秒表示,因此通常从6000开始并按1递增。

出于某种原因,最终的数字似乎是负面的。 我在添加操作结束时检查,最终计数小于Screenshot of sample out

我尝试将数字打印到控制台并且它们是正确的,我猜其他地方有问题吗?

var totalSeconds = 0; var minutesCounted = 0; var unzippedFolder = Compression.UnzipToFolder(zipPath); var listOfSeconds = ReadCsvIndex(unzippedFolder[0], ",", 0, true); foreach (var second in listOfSeconds) { // Console.WriteLine(Int32.Parse(second)); // Prints correct numbers totalSeconds += Int32.Parse(second); minutesCounted++; Console.WriteLine(minutesCounted + totalSeconds); } Console.WriteLine(security + totalSeconds); Console.WriteLine(minutesCounted); File.Delete(unzippedFolder[0]); if (totalSeconds > 1) { Console.WriteLine(true); } else { Console.WriteLine(false); // This is returning false } Console.ReadLine();

enter image description here

提前致谢。

{{1}}

1 个答案:

答案 0 :(得分:4)

long使用int值而不是默认totalSeconds。 该值显然高于Int32.MaxValue,因此值包围为负值。

long具有更高的最大值,因此您不会获得此溢出效果(至少不会很快)

相关问题