Floating Point Math Problem

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.
 
S

Stephany Young

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.
 
F

Family Tree Mike

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.
 

Ask a Question

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.

Ask a Question

Top