Floating Point Math Problem

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

I am attempting to solve a simple floating point math problem
and I am not getting the results that I think I should be getting.
Here is what I am doing:

Dim iMinutes As Integer
Dim dHours, dMinutes, dUnits As Double

iMinutes = DateDiff(DateInterval.Minute, dStartTime,
dEndTime)
dHours = iMinutes \ 60
dMinutes = iMinutes Mod 60
dUnits = dHours + (dMinutes \ 60.0)

My input start time is 9:00AM and my ending time is 10:15AM

dHours = 1 (good)
dMinutes = 15 (good)
dUnits = 1 (not good, should be 1.25)

Can someone tell me what I am doing wrong? It seems like I cannot
get any numbers to the right of the decimal point for dUnits.
 
You're NOT using the divide operator for floating point math.

You're using the division operator for integer math.

Use the / operator instead of the \ operator.
 
Joe said:
I am attempting to solve a simple floating point math problem
and I am not getting the results that I think I should be getting.
Here is what I am doing:

Dim iMinutes As Integer
Dim dHours, dMinutes, dUnits As Double

iMinutes = DateDiff(DateInterval.Minute, dStartTime,
dEndTime)
dHours = iMinutes \ 60
dMinutes = iMinutes Mod 60
dUnits = dHours + (dMinutes \ 60.0)

My input start time is 9:00AM and my ending time is 10:15AM

dHours = 1 (good)
dMinutes = 15 (good)
dUnits = 1 (not good, should be 1.25)

Can someone tell me what I am doing wrong? It seems like I cannot
get any numbers to the right of the decimal point for dUnits.

I think that dEndTime.Subtract(dStartTime).TotalHours is simpler.
 
Back
Top