M
Moussa2100
I want to know how to round a time in access or excel ?
i see miny topics but it didn't work
i see miny topics but it didn't work
Our resident mathematical genius James Fortune recently posted a very clever
solution for rounding. His idea can be wrapped in a little function for
Access like so:
Public Function RoundTo(dblVal As Double, dblTo As Double, Optional intUpDown
As Integer = -1) As Double
' rounds up by default.
' to round down pass 1 into function as
' optional intUpDown argument.
RoundTo = intUpDown * (Int(dblVal / (intUpDown * dblTo))) * dblTo
End Function
As the date/time data type is implemented a 64 bit floating point number,you
can use it for time, e.g. to round up the current date/time to the nearest 5
minutes:
Format(RoundTo(Now(),#00:05:00#),"dd mmmm yyyy hh:nn:ss")
which as I write at 14:17:44 currently returns:
27 February 2010 14:20:00
or:
Format(RoundTo(Now(),#00:05:00#,1),"dd mmmm yyyy hh:nn:ss")
to round down, which as I write returns:
27 February 2010 14:15:00
The value passed into the function as the dblTo argument would normally be an
even divisor of a minute, hour or day of course.
For Excel the equivalent formula would be, to round up:
=-INT(A1 / -(1/288)) * (1/288)
or to round down:
=INT(A1 / (1/288)) * (1/288)
where the date/time value is in A1. 1/288 is 5 minutes as fraction of a day
(24*12 = 288).
Ken Sheridan
Stafford, England
Our resident mathematical genius James Fortune recently posted a very
clever
solution for rounding. His idea can be wrapped in a little function for
Access like so:
Public Function RoundTo(dblVal As Double, dblTo As Double, Optional
intUpDown
As Integer = -1) As Double
' rounds up by default.
' to round down pass 1 into function as
' optional intUpDown argument.
RoundTo = intUpDown * (Int(dblVal / (intUpDown * dblTo))) * dblTo
End Function
As the date/time data type is implemented a 64 bit floating point number,
you
can use it for time, e.g. to round up the current date/time to the nearest
5
minutes:
Format(RoundTo(Now(),#00:05:00#),"dd mmmm yyyy hh:nn:ss")
which as I write at 14:17:44 currently returns:
27 February 2010 14:20:00
or:
Format(RoundTo(Now(),#00:05:00#,1),"dd mmmm yyyy hh:nn:ss")
to round down, which as I write returns:
27 February 2010 14:15:00
The value passed into the function as the dblTo argument would normally be
an
even divisor of a minute, hour or day of course.
For Excel the equivalent formula would be, to round up:
=-INT(A1 / -(1/288)) * (1/288)
or to round down:
=INT(A1 / (1/288)) * (1/288)
where the date/time value is in A1. 1/288 is 5 minutes as fraction of a
day
(24*12 = 288).
Ken Sheridan
Stafford, England
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.