ASP.Net - 数学运算和百分比 - VB

时间:2013-06-26 11:12:35

标签: asp.net vb.net

我有一个表格如下所示,我在每列中填写“通过”和“失败”记录的数量。然后我尝试计算每列的故障率以及3'IR室'设备和3'员工退出'设备的平均故障率。

Table

我使用本文底部的代码填充标签,但是我有2个问题: -

  1. 我收到的'输入字符串格式不正确'错误 第86行(Dim irAverageFailureRate as String)

  2. 我不禁想到有更好的方法可以做到这一点。我 ASP / VB的新手,所以关于如何改进这个的任何建议都是 感激地收到了。

    Sub getYesterdayStats()
    
    Dim con As OleDbConnection
    Dim cmdClockings As OleDbDataAdapter
    Dim dsClockings As New DataSet
    Dim yesterday As String = DateTime.Today.AddDays(-1).ToString("yyyy/MM/dd")
    
    
    con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; DATA SOURCE=\\XXX.XX.XXX.XX\XXXX.mdb;Jet OLEDB:Database Password=XXXXX;")
    cmdClockings = New OleDbDataAdapter("SELECT efrLog.PID, efrLog.LType, efrLog.LDName from efrLog WHERE LDate = '" & yesterday & "'", con)
    
    cmdClockings.Fill(dsClockings, "Clockings")
    
    ' Get pass and fail count for each device
    Dim ir1Pass As Integer = dsClockings.Tables("Clockings").Select("LType = 'PASS' AND LDName = 'IR Room 1'").Count
    Dim ir1Fail As Integer = dsClockings.Tables("Clockings").Select("LType = 'DENY' AND LDName = 'IR Room 1'").Count
    Dim ir2Pass As Integer = dsClockings.Tables("Clockings").Select("LType = 'PASS' AND LDName = 'IR Room 2'").Count
    Dim ir2Fail As Integer = dsClockings.Tables("Clockings").Select("LType = 'DENY' AND LDName = 'IR Room 2'").Count
    Dim ir3Pass As Integer = dsClockings.Tables("Clockings").Select("LType = 'PASS' AND LDName = 'IR Room 3'").Count
    Dim ir3Fail As Integer = dsClockings.Tables("Clockings").Select("LType = 'DENY' AND LDName = 'IR Room 3'").Count
    Dim ee1Pass As Integer = dsClockings.Tables("Clockings").Select("LType = 'PASS' AND LDName = 'Employee Exit 1'").Count
    Dim ee1Fail As Integer = dsClockings.Tables("Clockings").Select("LType = 'DENY' AND LDName = 'Employee Exit 1'").Count
    Dim ee2Pass As Integer = dsClockings.Tables("Clockings").Select("LType = 'PASS' AND LDName = 'Employee Exit 2'").Count
    Dim ee2Fail As Integer = dsClockings.Tables("Clockings").Select("LType = 'DENY' AND LDName = 'Employee Exit 2'").Count
    Dim ee3Pass As Integer = dsClockings.Tables("Clockings").Select("LType = 'PASS' AND LDName = 'Employee Exit 3'").Count
    Dim ee3Fail As Integer = dsClockings.Tables("Clockings").Select("LType = 'DENY' AND LDName = 'Employee Exit 3'").Count
    
    ' Calculate totals for each device
    Dim ir1total As Integer = ir1Pass + ir1Fail
    Dim ir2total As Integer = ir2Pass + ir2Fail
    Dim ir3total As Integer = ir3Pass + ir3Fail
    Dim ee1total As Integer = ee1Pass + ee1Fail
    Dim ee2total As Integer = ee2Pass + ee2Fail
    Dim ee3total As Integer = ee3Pass + ee3Fail
    
    ' Calcualte failure rate for each device
    Dim ir1failureRate As String = Format((ir1Fail / ir1Pass) * 100, "0.00")
    Dim ir2failureRate As String = Format((ir2Fail / ir2Pass) * 100, "0.00")
    Dim ir3failureRate As String = Format((ir3Fail / ir3Pass) * 100, "0.00")
    Dim ee1failureRate As String = Format((ee1Fail / ee1Pass) * 100, "0.00")
    Dim ee2failureRate As String = Format((ee2Fail / ee2Pass) * 100, "0.00")
    Dim ee3failureRate As String = Format((ee3Fail / ee3Pass) * 100, "0.00")
    
    ' Calculate average failure rate for IR and EE devices
    Dim irAverageFailureRate As String = Format((Format((ir1Fail / ir1Pass) * 100, "0.00") + Format((ir2Fail / ir2Pass) * 100, "0.00") + Format((ir3Fail / ir3Pass) * 100, "0.00")) / 3, "0.00")
    Dim eeAverageFailureRate As String = Format((Format((ee1Fail / ee1Pass) * 100, "0.00") + Format((ee2Fail / ee2Pass) * 100, "0.00") + Format((ee3Fail / ee3Pass) * 100, "0.00")) / 3, "0.00")
    
    ' Assign values to labels
    lblIR1pass.Text = ir1Pass
    lblIR1fail.Text = ir1Fail
    lblIR2pass.Text = ir2Pass
    lblIR2fail.Text = ir2Fail
    lblIR3pass.Text = ir3Pass
    lblIR3fail.Text = ir3Fail
    lblEE1pass.Text = ee1Pass
    lblEE1fail.Text = ee1Fail
    lblEE2pass.Text = ee2Pass
    lblEE2fail.Text = ee2Fail
    lblEE3pass.Text = ee3Pass
    lblEE3fail.Text = ee3Fail
    lblIR1tot.Text = ir1total
    lblIR2tot.Text = ir2total
    lblIR3tot.Text = ir3total
    lblEE1tot.Text = ee1total
    lblEE2tot.Text = ee2total
    lblEE3tot.Text = ee3total
    lblIR1fr.Text = ir1failureRate & "%"
    lblIR2fr.Text = ir2failureRate & "%"
    lblIR3fr.Text = ir3failureRate & "%"
    lblEE1fr.Text = ee1failureRate & "%"
    lblEE2fr.Text = ee2failureRate & "%"
    lblEE3fr.Text = ee3failureRate & "%"
    lblIRafr.Text = irAverageFailureRate & "%"
    lblEEafr.Text = eeAverageFailureRate & "%"
    

    End Sub

1 个答案:

答案 0 :(得分:2)

该行中有许多不必要的格式。

Dim irAverageFailureRate As String = ((CDbl(ir1Fail) / ir1Pass) + (CDbl(ir2Fail) / ir2Pass) + (CDbl(ir3Fail) / ir3Pass) / 3).ToString("P")

我从Standard Numeric Format Strings获得了百分比“P”。