Search code examples
libreofficefloating-accuracybasic

LibreOffice floating point precision


I am doing calculations on date and time using floating-point numbers. However, I notice that the calculated value is not as expected. Here is the code snippet.

Dim sTemp As Single, sLineDateTime As Single
Dim strTemp As String

strTemp = "2019-02-25"
sLineDateTime = DateValue(DateSerial(Left(strTemp, 4), Mid(strTemp, 6, 2), Right(strTemp, 2)))
strTemp = ""21:47:42"
REM TODO Time is being rounded off. Check
sTemp = TimeValue(TimeSerial(Left(strTemp, 2), Mid(strTemp, 4, 2), Right(strTemp, 2)))
sLineDateTime = sLineDateTime + sTemp

The output of the above computation is sLineDateTime="43521.906250" which when converted to date/time is "Mon 25-Feb-2019 09:45:00 PM". The actual value expected is "43521.908125" which translates to error of "0.001875000001746" or in other words 2 minutes 42 seconds. Any suggestions on how I can overcome this problem?


Solution

  • I did try before posting this and it did not help. I switched to using dates which is working perfectly for me now. Here is the code.

            strTemp = "2019-02-25"
    dLineDateTime = DateSerial(Left(strTemp, 4), Mid(strTemp, 6, 2), Right(strTemp, 2))
    strTemp = "21:47:42"
    dLineDateTime = dLineDateTime + TimeSerial(Left(strTemp, 2), Mid(strTemp, 4, 2), Right(strTemp, 2))