1

How can I stop a decimal from being truncated in Visual Basic, and keep it as a full decimal rather than a truncated number in scientific notation?

I have this little bit of code from am Excel Macro that I'm modifying for someone (only the Debug.Print statements are mine):

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

The Debug.Print statements give me:

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

So basically what is happening here is the values are being pulled from the Excel Worksheet in scientific notation, and everything not shown in that notation is getting truncated.

The actual number should be 1343300687515.39, but it is being forced down to 1.343301E+12, and even if I force it out of scientific notation, it's still giving me 1343301000000.

How can I get it to be the actual number with decimal points and all? This aspect is practically required because in some instances, there might only be a difference of 20 or so, but since it's truncating to the millions place, it's completely useless.

Edit: Here's the entire function for reference

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
4

1 回答 1

1

您的问题与变量的大小有关。如果rttimeSingle,它不足以容纳您需要的数据。将变量类型更改为Double应该可以解决问题。

作为一个经验法则,除非您有非常严格的内存限制,否则请使用Long来保存整数值和Double浮点值。保留Decimal类型以进行非常精确的计算。我还建议您避免Variant使用类型。

于 2013-05-08T05:33:11.477 回答