Rounding

  • Thread starter Thread starter Stan
  • Start date Start date
S

Stan

I need know how to round down. If I have a calculation and
the number comes out to 4.74 I would like the number to be
4 instead of 5. When I use the round operator it is
rounding up. I would like to always round down.

I have the number of days remaining in a term which 2920
which is 8.41 years. I would like know the number of full
years and full month and the remaining days.

To do this I am calculating the number of days in a full
year. I would perform the same operation for full months.

I think there maybe an easier way to do this.
 
You could use the Fix or Int functions. For positive numbers, they both
produce the same result, but for negative numbers the results are
different - Fix simply discards the fractional part, while Int rounds toward
zero ...

? fix(1.9)
1
? int(1.9)
1
? fix(-1.9)
-1
? int(-1.9)
-2

Alternatively, you may be able to adapt the following code, which I wrote in
reply to a similar question. That question involved days, hours, minutes and
seconds rather than years and days, but the principle, using integer
division and modulus, is the same ...

Public Function DaysHoursMinutesSeconds(ByVal StartDate As Date, ByVal
EndDate As Date) As String

Dim lngSeconds As Long
Dim lngMinutes As Long
Dim lngHours As Long
Dim lngDays As Long
Dim strResult As String

lngSeconds = DateDiff("s", StartDate, EndDate)
lngMinutes = lngSeconds \ 60
lngSeconds = lngSeconds Mod 60
lngHours = lngMinutes \ 60
lngMinutes = lngMinutes Mod 60
lngDays = lngHours \ 24
lngHours = lngHours Mod 24

strResult = Format$(lngDays, "00") & ":" & Format$(lngHours, "00") & ":"
& _
Format$(lngMinutes, "00") & ":" & Format$(lngSeconds, "00")

DaysHoursMinutesSeconds = strResult

End Function

A quick test in the Immediate window ...
? dayshoursminutesseconds(#2 feb 2005 17:30:30#, #3 feb 2005 18:35:35#)
01:01:05:05
 
Back
Top