我有兩個欄位,在表屬性中都將大小設定為兩倍。當我從另一個欄位中減去一個欄位時,當我單擊單元格時,一些結果會顯示為科學記數法,而其他結果只是將常規標準格式顯示為小數位。
兩個欄位中的資料都使用 Round([Field01],2) 和 Round([Filed2],2) 進行了更新,因此欄位中的數字不應超過小數點后 2 位。
這是一個示例:Field1 = 7.01 Field2 = 7.00 但是當我從 Field2 中減去 Field1 時,訪問顯示顯示 0.01,但是當我單擊顯示的結果時,顯示 -9.99999999999979E-03。所以當然,當我嘗試過濾所有具有 0.01 的結果時,查詢回傳為空,因為它認為結果是 -9.99999999999979E-03。
更奇怪的是,如果 Field1 = 1.02 和 Field2 = 1.00,結果是 0.02,當我點擊結果時,顯示仍然顯示 0.02,我可以過濾所有等于 0.02 的結果。
為什么 MS Access 會以不同的方式處理同一查詢中的數字?為什么它以科學計數法顯示而不是過濾?
感謝您的任何支持。
uj5u.com熱心網友回復:
在 Access(甚至 Excel)中獲取這個簡單的代碼并運行它!
Public Sub TestAdd()
Dim MyNumber As Single
Dim I As Integer
For I = 1 To 10
MyNumber = MyNumber 1.01
Debug.Print MyNumber
Next I
End Sub
這是上面的輸出:
1.01
2.02
3.03
4.04
5.05
6.06
7.070001
8.080001
9.090001
10.1
您可以看到,在進行了 7 次加法運算后,舍入發生了!
請注意,在僅 7 次簡單的添加之后,Access 現在會吐出錯誤的數字并出現舍入錯誤!
更神奇?上面的代碼在 Excel 中運行相同!
好的,我相信我現在已經引起了您的注意!
如果我記得,計算科學的第一天和第一堂課?使用浮點數時,計算機不會存盤精確的數字。
那么,整個使用 Excel、Access 或實際上您的桌面計算器的商業社區怎么可能不會崩潰呢?
您的意思是 Access 不能將 7 個簡單的小數字相加而不會出錯?
那我怎么能做工資單呢?
基本概念和您需要知道的所有內容是計算機僅將實數(浮動)數字存盤為近似值。
整數值是精確存盤的。
所以,這里有幾種方法,事實上,如果你撰寫任何需要處理金錢價值的商業軟體?并且不會遭受舍入錯誤?
那么你最好選擇我們所說的某種“縮放”整數。在幕后,計算機不使用浮點數,而是使用整數值,并且還有一個“小數”位置。
In fact, in a lot of older business BASIC languages, or others? We often had to do the scaling on our own. (so, we would choose a large integer format). In fact, this "scaling" feature still exists in Access!!! (and you see it in the format options).
So, two choices here. If you don't want "tiny" rounding errors, then use "currency" data type. This may, or may not be sufficient for you, since it only allows a max of 4 decimal places. But in most cases, it should suffice. And if you need "more" decimal places, then you can multiply the values by 1000, and then divide by 1000 when done the calculations.
however, try changing the column type to currency and that should work. (this type of data is how your desktop calculator also works - and thus you not see funny rounding errors as a result (in most cases).
but, the FIRST rule of the day? First computer course?
Computers do not store exact numbers for floating point numbers - they are approximations, and are subject to rounding errors. Now, if you really are using double for the table, then I don't think these rounding errors should show up - since you have "so many decimal places" available.
But, I would try using currency data type - it is a scaled integer, or so called packed decimal.
You can ALSO choose to use a packed decimal in Access, and it supports out to 28 digits, and you can set the "scale" (the decimal point location). However, since you can't declare a decimal type in VBA, then I would suggest that in the table (and in VBA code, use currency data types).
如果您需要超過 4 個小數點,則考慮在代碼中縮放貨幣,或者可能在此時,您考慮在表中使用壓縮十進制型別,但 VBA 中的值必須使用“變體”型別,并且如果在代碼中使用并從相關表中分配值,它們將正確地采用資料列設定。
不用說,你開始與電腦打交道的第一天,除了成為“最終用戶”之外還有什么?好吧,這是你今天的第一課!
uj5u.com熱心網友回復:
“兩個欄位中的資料都使用 Round([Field01],2) 和 Round([Filed2],2) 進行了更新,因此欄位中的數字不應超過小數點后 2 位。” 而不是四舍五入(我認為這是科學記數法的原因),您可以使用數字欄位作為資料型別,然后在欄位大小下選擇雙精度,然后在小數位下選擇 2。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/451417.html
