不准确的十进制转换为小时

时间:2018-02-14 03:27:14

标签: vb.net

伙计们请看这段代码,我找不到有什么问题。 我试图将十进制值转换为小时和分钟,但似乎不准确。

        Dim selReleased As New SqlDataAdapter("SELECT SUM(TotalHours) AS ProcessingTime FROM TimeConsumed WHERE ClientID ='" & ClientAccountStatusViewer.txtClientID.Text & "'", jonsqlcon)
        Dim setReleased As New DataSet
        selReleased.Fill(setReleased)
        Dim txtPtime As String
        Dim ProcessingTime As Decimal

        txtPtime = setReleased.Tables(0).DefaultView.Item(0).Item("ProcessingTime")
        ProcessingTime = Math.Round(Convert.ToDecimal(txtPtime), 2)

        Dim pTime As String = String.Format("{0}:{1}", CInt(ProcessingTime), (ProcessingTime Mod 1) * 60)

        MessageBox.Show("Time Consumed: " & pTime, "RELEASED", MessageBoxButtons.OK, MessageBoxIcon.Information)

当代码执行时,它会将数据库中所有记录的TotalHours总结为ProcessingTime,然后它将执行我使用的公式String.Format("{0}:{1}", CInt(ProcessingTime), (ProcessingTime Mod 1) * 60),但它似乎是准确的。

TotalHours的总和是:0.63所以它超过30分钟。

但是如果你看表​​从第一个记录的TimeStart到TimeStopped的最后一个记录与TotalHours不匹配,即0.63和大约18-19分钟。任何帮助都感激不尽。在此先感谢!

供参考,请查看此表:

ClientID        TimeStart               TimeStopped             Officer          StageProcess       TotalHours      Status
UO.1802.0002    2/13/2018 1:52:16 PM    2/13/2018 1:53:18 PM    Jeff Olive       Check List         .01747  
UO.1802.0002    2/13/2018 1:53:18 PM    2/13/2018 1:53:23 PM    Jeff Olive       Application        .00153  
UO.1802.0002    2/13/2018 1:53:23 PM    2/13/2018 1:53:40 PM    Jeff Olive       Occular            .00482  
UO.1802.0002    2/13/2018 1:53:40 PM    2/13/2018 1:59:24 PM    jon ra           Remarks Step2PQ    .09558          For Verification
UO.1802.0002    2/13/2018 1:59:24 PM    2/13/2018 2:00:14 PM    Jeff Olive       Occular            .1143   
UO.1802.0002    2/13/2018 2:00:14 PM    2/13/2018 2:01:08 PM    jon ra           Remarks Step2PQ    .12455  
UO.1802.0002    2/13/2018 2:01:08 PM    2/13/2018 2:02:11 PM    Jayson Tadeo     CIR                .0176   
UO.1802.0002    2/13/2018 2:02:11 PM    2/13/2018 2:02:32 PM    Jayson Tadeo     AR                 .00596  
UO.1802.0002    2/13/2018 2:02:32 PM    2/13/2018 2:03:20 PM    jon ra           Remarks Step4PV    .01351          For Verification
UO.1802.0002    2/13/2018 2:03:20 PM    2/13/2018 2:04:19 PM    Jayson Tadeo     CIR                .01651  
UO.1802.0002    2/13/2018 2:04:19 PM    2/13/2018 2:04:27 PM    Jayson Tadeo     AR                 .00233  
UO.1802.0002    2/13/2018 2:04:27 PM    2/13/2018 2:05:09 PM    jon ra           Remarks Step4PV    .04373  
UO.1802.0002    2/13/2018 2:05:09 PM    2/13/2018 2:06:27 PM    Jeff Olive       CRAM               .02182  
UO.1802.0002    2/13/2018 2:06:27 PM    2/13/2018 2:07:34 PM    jon ra           Remarks Step6CA    .01873          For Verification
UO.1802.0002    2/13/2018 2:07:34 PM    2/13/2018 2:08:26 PM    Jeff Olive       CRAM               .05493  
UO.1802.0002    2/13/2018 2:08:26 PM    2/13/2018 2:09:43 PM    jon ra           Remarks Step6CA    .05465  
UO.1802.0002    2/13/2018 2:09:43 PM    2/13/2018 2:11:13 PM    Jeff Olive       LoanDoc            .02505  

1 个答案:

答案 0 :(得分:1)

为什么重新发明轮子?

在处理时间时,最好使用TimeSpan类。

    ProcessingTime = Math.Round(Convert.ToDecimal(txtPtime), 2)

    Dim ts As TimeSpan = TimeSpan.FromHours(ProcessingTime)

    Dim pTime As String = ts.TotalDays.ToString("f0") & " day(s) " &
                    ts.Hours.ToString.PadLeft(2, "0"c) & ":" &
                    ts.Minutes.ToString.PadLeft(2, "0"c) & ":" &
                    ts.Seconds.ToString.PadLeft(2, "0"c)

pTime = "0 day(s) 00:37:48",就这么简单。但你可以随心所欲地柚木。