2013-05-07 58 views
1

如何阻止小數在Visual Basic中被截斷,並以科學記數法將其保留爲完整小數而不是截斷數字?在Visual Basic中停止十進制截斷

我有從我修改有人上午Excel宏代碼這一點(只有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,即使我逼出來的科學記數法的,它仍然給我13433.01億

我怎樣才能得到它與小數點和所有的實際數字?這方面實際上是需要的,因爲在某些情況下,可能只有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 
+0

我認爲它與Excel的數值精度有關(順便說一下,這不是很好)...... Excel中非常大(或非常小)的數字被截斷 – Barranka 2013-05-07 23:27:55

+0

@Barranka我實際上已經設置了電子表格集不截斷數字。在電子表格中,它顯示了整個數字,小數點和全部。 – 2013-05-07 23:34:13

+0

這是'rttime'的類型? (我試圖重現這種情況) – Barranka 2013-05-07 23:38:41

回答

1

您的問題是否與變量的大小做了整個功能。如果rttimeSingle,它不足以容納您需要的數據。將變量類型更改爲Double應該可以解決問題。

作爲一個縮略圖規則,除非您有非常嚴格的內存限制,否則請使用Long來保存整數值,使用Double來保存浮點值。保留Decimal類型以進行非常精確的計算。我也建議你避免Variant類型。

+0

謝謝!我們也在昨晚的評論中發現了這一點。我從一個不再在我們的實驗室工作的人那裏繼承這個代碼,我只是在進行修改,但在這裏使用了一些相當不好的做法。 – 2013-05-08 15:51:16