在Visual Basic中停止Decimal截断

如何停止在Visual Basic中截断小数,并将其保留为完整的十进制而不是以科学记数法截断的数字?

我有一些Excelmacros的代码,我正在修改某人(只有Debug.Print语句是我的):

rttime = asht.Cells(i, timecol).Value Debug.Print "rttime=" & rttime Debug.Print "To string: " & CStr(CDec(rttime)) rtdelta = rttime - wavonset_value 'searchcol="AOISlide2" Debug.Print "rtdelta=" & rtdelta 

Debug.Print语句给我:

 rttime=1.343301E+12 To string: 1343301000000 rtdelta=0 

所以基本上这里发生了什么是从科学记数法的Excel工作表中拉出的值,并没有在该表示法中显示的所有内容都被截断。

实际数字应该是1343300687515.39 ,但是被迫下降到1.343301E + 12 ,即使我用科学计数法强制它,它仍然给我1343301000000

我怎么能得到它是小数点和所有的实际数字? 这个方面实际上是需要的,因为在某些情况下,可能只有20个左右的差别,但是由于它被截断到数百万个地方,所以它是完全没有用的。

编辑:这是整个函数的参考

 Function GetAOIRowStart(asht As Worksheet, startrow&, endrow&, searchstr$, searchcol&, AOItime_start As Single) As Long Dim i& Dim rtdelta As Single Dim rttime As Single Dim wavonset_value As Single Dim timecol& timecol = 4 wavonset_value = asht.Cells(startrow, timecol).Value 'tettime Debug.Print "wavonset_value=" & wavonset_value i = startrow Do Until i >= endrow 'scan down TeTTime' column to find nearest look of interest rttime = asht.Cells(i, timecol).Value Debug.Print "rttime=" & rttime Debug.Print "To string: " & CStr(CDec(rttime)) rtdelta = CDec(Right(rttime, 9)) - CDec(Right(wavonset_value, 9)) 'searchcol="AOISlide2" Debug.Print "rtdelta=" & rtdelta If rtdelta >= AOItime_start Then Exit Do End If i = i + 1 Loop GetAOIRowStart = i 'Debug.Print asht.Cells(i, sound_playing_col).Value End Function 

你的问题与variables的大小有关。 如果rttimeSingle ,这不足以容纳你需要的数据。 将variablestypes更改为Double应该可以解决问题。

作为一个规则,除非有非常严格的内存限制,否则使用Long来保存整数值,使用Double来保存浮点值。 保留Decimaltypes进行非常精确的计算。 我也build议你避免Varianttypes。