長話短說,我工作的地方用時鐘的象限來測量時間。例如,1.1是1小時0-15分鐘,1.2是1小時15-30分鐘,1.3是1小時30-45分鐘。沒有1.4,因爲1.4當然等於2.在UDF中使用Excel的舍入舍入1到0
我想製作一個Excel表格,它會自動添加我的時間在這個系統下,所以我編寫了這個UDF來轉換時間,通過分隔十進制值和乘乘以2.5得到一個正常的十進制值(.1 = .25,.2 = .5,.3 = .75),然後在最後除以2.5,轉換回我的僱主格式。我知道可以使用Excel的現有公式來完成,但是有點混亂,說實話我太固執了,現在就讓它走。
如果你看下面的屏幕截圖,你會發現該函數適用於除了最後一週的總列之外的所有列,出於某種原因顯示39.4而不是40(兩個值在技術上相同,但是由於某種原因,程序不會將.4轉換爲1)。
http://i.imgur.com/yxOvlkP.png
這裏是它的全部代碼。這個問題似乎在餘數等於1的情況下出現(爲了簡單起見,只需要輸入兩個結尾爲0.2的值),然後在最後以某種方式舍入到零。
Function newMath(week As Range) As Double
Dim time As Variant
Dim remainder As Double
Dim wholeTime As Double
remainder = 0
wholeTime = 0
For Each time In week
remainder = remainder + ((time - WorksheetFunction.RoundDown(time, 0)) * 2.5) 'Separate and sum up decimal values
wholeTime = wholeTime + WorksheetFunction.RoundDown(time, 0) 'Separate and sum up whole hours
Next time
'Problem occurs at this point when remainder = 1
'WorksheetFunction.RoundDown(remainder, 0) will equal 0 below even when 1 should round down to 1
wholeTime = wholeTime + WorksheetFunction.RoundDown(remainder, 0) 'Add the whole remainder hours to whole time
remainder = (remainder - WorksheetFunction.RoundDown(remainder, 0))/2.5 'Get decimal value of remainder and convert back to quadrant
newMath = wholeTime + remainder
End Function
不知怎的,當餘正好等於1 Excel的ROUNDDOWN功能似乎是四捨五入到0
這意味着,因爲它應該下面的行不會將1添加到整數倍:
wholeTime = wholeTime + WorksheetFunction.RoundDown(remainder, 0)
而且,當它不應該(這是.4來自)這條線將返回1,它得到了2.5分:
remainder = (remainder - WorksheetFunction.RoundDown(remainder, 0))/2.5
我不確定發生了什麼事情,或者爲什麼excel會將我的餘數從1減小到0,如果這確實是問題所在。我很感激任何幫助,如果您需要更多信息,請告訴我們。謝謝!
你的問題是,電腦是二進制和你正在做數字算術。關於39.4的結果,其餘的計算爲0.399999999999999你需要舍入,或使用十進制算術 –