2017-05-14 119 views
0

我在嘗試計算6個項目的總體平均值。其中一些項目可能爲零,因此在計算平均值時不會發揮作用。平均值完全讓我感到困惑,並且在此過程中遇到困難。基於數組計算AVG

我的代碼,因爲我現在有它:

 Dim a, 
     af1, af2, af3, af4, af5, af6, 
     t7, t8, t9, t10, t11, t12 As Decimal 

     'Adjusted Fuel Cost per line 
    af1 = CDec(IIf(tbAdjCostPerGal1.Text.Trim = "", 0D, tbAdjCostPerGal1.Text.Trim)) 
    af2 = CDec(IIf(tbAdjCostPerGal2.Text.Trim = "", 0D, tbAdjCostPerGal2.Text.Trim)) 
    af3 = CDec(IIf(tbAdjCostPerGal3.Text.Trim = "", 0D, tbAdjCostPerGal3.Text.Trim)) 
    af4 = CDec(IIf(tbAdjCostPerGal4.Text.Trim = "", 0D, tbAdjCostPerGal4.Text.Trim)) 
    af5 = CDec(IIf(tbAdjCostPerGal5.Text.Trim = "", 0D, tbAdjCostPerGal5.Text.Trim)) 
    af6 = CDec(IIf(tbAdjCostPerGal6.Text.Trim = "", 0D, tbAdjCostPerGal6.Text.Trim)) 

      'Truck Gallons Purchased Related 
    t7 = CDec(IIf(tbTrkGalsPurch1.Text.Trim = "", 0D, tbTrkGalsPurch1.Text.Trim)) 
    t8 = CDec(IIf(tbTrkGalsPurch2.Text.Trim = "", 0D, tbTrkGalsPurch2.Text.Trim)) 
    t9 = CDec(IIf(tbTrkGalsPurch3.Text.Trim = "", 0D, tbTrkGalsPurch3.Text.Trim)) 
    t10 = CDec(IIf(tbTrkGalsPurch4.Text.Trim = "", 0D, tbTrkGalsPurch4.Text.Trim)) 
    t11 = CDec(IIf(tbTrkGalsPurch5.Text.Trim = "", 0D, tbTrkGalsPurch5.Text.Trim)) 
    t12 = CDec(IIf(tbTrkGalsPurch6.Text.Trim = "", 0D, tbTrkGalsPurch6.Text.Trim)) 

      'Calculate ADJUSTED Average Cost of ALL Fuel plus Fuel Card fee and any Fuel Discounts this Load 
    Try 
     If af1 > 0 Then 
      a = (af1 + af2 + af3 + af4 + af5 + af6)/(t7 + t8 + t9 + t10 + t11 + t12) 
      tbFuelCostAdj.Text = a.ToString("C3") 
     Else 
      a = 0D 
      tbFuelCostAdj.Text = a.ToString("C3") 
     End If 
    Catch ex As Exception 
     'If a calculation error occurs, show Error message box 
     Dim frm As New MeMsgCalcError(ex, "'Adjusted Cost of Fuel this Load' Calculation Error." & vbCrLf & "Lines 644-652") 
     frm.Show() 
    End Try 

這是我試圖獲得平均我的代碼的相關部分。

我有作爲一個例子:

  • tbAdjCostPerGal1 = $ 2.259
  • tbAdjCostPerGal2 = $ 2.469
  • tbAdjCostPerGal3 = $ 0.000
  • tbAdjCostPerGal4 = $ 0.000
  • tbAdjCostPerGal5 = $ 0.000
  • tbAdjCostPerGal6 = $ 0.000

  • tbTrkGalsPurch1 = 100.000

  • tbTrkGalsPurch2 = 93.000
  • tbTrkGalsPurch3 = 0.000
  • tbTrkGalsPurch4 = 0.000
  • tbTrkGalsPurch5 = 0.000
  • tbTrkGalsPurch6 = 0.000

基於該信息的上方,只第2項(tbAdjCostPerGal)將被添加,然後平均,因爲項目3-6是一個0值,結束w最終每加侖平均成本爲2.364美元。

所以總而言之,我需要總共和平均所有的tbAdjCostPerGal.text > 0。我相信如果我平均只有tbAdjCostPerGal條目,這將給我我正在尋找的答案。每條記錄總是有6個條目。

我知道我需要創建一些方法來創建一個變量來查看tbAdjCostPerGal條目,總計只有那些值大於0的條目,這是我缺乏知識的地方。有人可以幫助我正確地格式化這個嗎?

+1

零是一個合法的值 - 爲什麼它不會發揮作用? – Plutonix

+1

看起來這是平均燃料價格。我猜燃料價格爲0.0美元意味着沒有價格記錄而不是燃料是免費的。 (我必須去哪裏填補這些燃料?)因此,在計算平均燃料成本的情況下,您確實想要排除$ 0值。在這種情況下,應該使用更好的「沒有價格記錄」的信號量。但是因爲我們不需要使用0.0美元的信號量。 – David

回答

1

我猜你需要總價格除以加侖數。例如:

Total Price = $2.259 * 100.000 + $2.469 * 93.000 + $0.000 * 0.000 + ... = $455.517 

Average per Gallon = Total Price/# of Gallons = 455.517/(100 + 93 + ...) = 2.36019170984 

爲平均不在0的值,則可以通過值的數量劃分總和不在0:

Dim array = { af1, af2, af3, af4, af5, af6 } 

Dim count = array.Count(Function(d) d <> 0) 

Dim average = IIF(count = 0, 0, array.Sum()/count) 

或篩選出0值:

tbFuelCostAdj.Text = array.Where(Function(d) d <> 0).Average.ToString("C3") 
+0

你的猜測是正確的。是的,0.000或$ 0.000意味着沒有價格或燃料在該線上購買。否則,像你一樣,我會每天在那裏購買燃料!默認值始終爲0,但無法確定,因爲分頻器總是6,這就是我失敗的原因。我只需要它除以值> 0的數字。 –

+0

這個效果很好,只有一個例外......如果一個記錄沒有購買燃料,無論如何,代碼崩潰......除以零誤差。因此,需要在檢查中工作以確保在計算之前存在某種東西。我嘗試在try語句中添加內容:if af1> 0 then ....但這沒有什麼區別,仍然得到Divide by Zero錯誤。 –

+0

@JohnEtling如此檢查除數是否爲0之前 – Slai