473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function for converting to minutes in decimal?

I have column of data that have elapsed time in minutes.
For example:
1:15
1:47
23:12

I like to be able to use sql server to do simple math functions. Does
anyone have a simple function/formula that will convert into a decimal
minutes form?

EG: 1:30 should equal 1.5 minutes

Thanking you.

Masa
Nov 20 '05 #1
5 16868
Once you have the Hours (whole number part of the value), Minutes, and
Seconds (if you are using Seconds as well), the formula is:

Value = Hours +( (Minutes + Seconds/60)/60)

If you are not using Seconds, then you would use:

Value = Hours + (Minutes/60)

Jody
"Masahiro Ito" <ma**@pleasespamgoaway.it> wrote in message
news:Xn************************@216.196.105.130...
I have column of data that have elapsed time in minutes.
For example:
1:15
1:47
23:12

I like to be able to use sql server to do simple math functions. Does
anyone have a simple function/formula that will convert into a decimal
minutes form?

EG: 1:30 should equal 1.5 minutes

Thanking you.

Masa

Nov 20 '05 #2
"Jody Gelowitz" <jo**@mail.visualstatement.com> wrote in news:
Once you have the Hours (whole number part of the value), Minutes, and
Seconds (if you are using Seconds as well), the formula is:
Value = Hours +( (Minutes + Seconds/60)/60)
If you are not using Seconds, then you would use:
Value = Hours + (Minutes/60)


Thank you Jody.

I am trying this formula, but have trouble with it.
my numbers come from a text file like: 1:15, 1:45, 10:37, 123:14. They
are always minutes, then seconds.

My code looks much more complicated than your formula, but I can't seem
to make your formula work?

Here is my code. I think I got it working, but it runs quite slow - I do
this to millions of numbers.
Public Function Time2Decimal(ByVal s As String)
Dim i As Integer
Dim dbl As Double
Dim str() As String = s.Split(":")
If str(1) = "00" Then
dbl = CType(str(0), Double)
Else
Dim dbl2 As Double = CType(str(1), Double)
dbl2 = dbl2 / 60
str(1) = dbl2.ToString
Dim split() As String = str(1).Split(".")
If split(1).Length > 2 Then
split(1) = split(1).Remove(2, split(1).Length - 2)
End If
Dim sFin As String = str(0) & "." & split(1)
dbl = CType(sFin, Double)
End If
Return dbl
End Function

Masa
Nov 20 '05 #3
"Masahiro Ito" <ma**@pleasespamgoaway.it> wrote...
My code looks much more complicated than your formula, but I can't seem
to make your formula work?


This should work:

Private Function TimeToMinutes(ByVal s As String) As Double
Dim strs() As String = s.Split(":"c)
MinSeconds = CDbl(strs(0)) + (CDbl(strs(1)) / 60)
End Function

Test it with this:

Console.WriteLine(TimeToMinutes("1:15").ToString)
Console.WriteLine(TimeToMinutes("2:45").ToString)
Console.WriteLine(TimeToMinutes("30:37").ToString)
Console.WriteLine(TimeToMinutes("123:14").ToString )

By the way... if it is possible that the string could be "0" or even "15"
with no ":" separator it is a good idea to avoid adding additional code in
the algorithm to handle it. Rather conform the data right away and use the
common routine. So for instance if you do need to check for zero or a
single value (you have to decide if a single value represents minutes or
seconds) then check for the separator and if there isn't one then just add
it. Pass this along to the converter. If you need to you can place those
lines of code in TimeToMinutes but they should appear first... not
interspersed inside.

Tom

Nov 20 '05 #4
"Tom Leylan" <ge*@iamtiredofspam.com> wrote...

Oh wouldn't you know... I changed the function name as I posted the code and
missed the return value...
Private Function TimeToMinutes(ByVal s As String) As Double
Dim strs() As String = s.Split(":"c)
MinSeconds = CDbl(strs(0)) + (CDbl(strs(1)) / 60)
End Function


Here it is again with the lines that will handle "0" or "15" (assuming any
value without the ":" separator is seconds. It is easy to make it assume
minutes if you need that instead.

Private Function TimeToMinutes(ByVal s As String) As Double

If s.IndexOf(":"c) < 0 Then
s = "0:" & s
End If

Dim strs() As String = s.Split(":"c)
Return Convert.ToDouble(strs(0)) + (Convert.ToDouble(strs(1)) / 60)

End Function
Console.WriteLine(TimeToMinutes("0").ToString)
Console.WriteLine(TimeToMinutes("15").ToString)

Console.WriteLine(TimeToMinutes("1:15").ToString)
Console.WriteLine(TimeToMinutes("2:45").ToString)
Console.WriteLine(TimeToMinutes("30:37").ToString)
Console.WriteLine(TimeToMinutes("401:20").ToString )
Nov 20 '05 #5
"Tom Leylan" <ge*@iamtiredofspam.com> wrote in
Here it is again with the lines that will handle "0" or "15" (assuming
any value without the ":" separator is seconds. It is easy to make it
assume minutes if you need that instead.
Private Function TimeToMinutes(ByVal s As String) As Double
If s.IndexOf(":"c) < 0 Then
s = "0:" & s
End If
Dim strs() As String = s.Split(":"c)
Return Convert.ToDouble(strs(0)) + (Convert.ToDouble(strs(1))
/ 60)


Very very thank you! perfect code - much better than mine.
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
9
by: Bill L. | last post by:
We recently noticed that the vb.net CStr function yields different results than the vb6 version when converting SQL decimal data. If, for example, the data is defined in SQL as decimal(19,10), the...
17
by: nomenklatura | last post by:
Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then...
6
by: Harlin Seritt | last post by:
I would like to take milliseconds and convert it to a more human-readable format like: 4 days 20 hours 10 minutes 35 seconds Is there something in the time module that can do this? I havent...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
2
by: dwasbig9 | last post by:
Hi Group (fairly limited knowledge of Access and almost none of Access VBA. Using Access 2003). I need to sum time, I've found through the groups archive an sql extract that led me to this ...
4
by: Jeff | last post by:
Hey ..NET 2.0 In my code am I trying to convert an int value into a decimal: decimal d = 0; int k = 87664; d = Convert.ToDecimal(k/100);
3
by: HowHow | last post by:
I am using Excel 2000 to calculate the pay for worker and there are 4 possibility: 1.If actual work time is within 5 minutes under rostered time, pay rostered time 2.If actual work time is 5...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.