Convert extended value to Time

1.6k Views Asked by At

I want to convert a extended value to time. before the DecimalSeparator is hours after the decimalseparator are minutes digital

8,62944444444444  --> 8:37
1,41666666666667  --> 1:25

I've made this funtion but I get for 1,41666666666667 --> 1:24 It must be 25 minutes

function MyConvertion(AValue: Extended): TDateTime;
var
    Hour, Min, Sec, MSec: Word;
begin
  Hour:= Trunc(AValue);
  Min:= Trunc(Frac(AValue)*60);
  result:= EncodeTime(Hour, Min, 0, 0);
end;

When I use exact 1,41666666666667 I'm get 1:25 But the value I must convert comes out from a soap enterface as double (oPostCalculationDay.DirectAssignedWorkTime).

The value I get in debug mode = 1,41666666666667

MyConvertion(1,41666666666667) --> 1:25
MyConvertion(oPostCalculationDay.DirectAssignedWorkTime) --> 1:24
MyConvertion(RoundTo(oPostCalculationDay.DirectAssignedWorkTime, -15)) --> 1:25
MyConvertion(RoundTo(oPostCalculationDay.DirectAssignedWorkTime, -16)) --> 1:24
MyConvertion(strtofloat(floattostr(oPostCalculationDay.DirectAssignedWorkTime)))--> 1:25

I'm thinking is something like Floating points

But what is the best methode I must use? Roundto -14 because 1,41666666666667 is 14 numbers after the separator of something else?

1

There are 1 best solutions below

8
David Heffernan On

My assumption is that the values are encoded like this:

Hours + Minutes/60 

where Hours and Minutes are integers and Minutes is between 0 and 59 inclusive.

You need to round the minutes rather than truncate them

Min := Round(Frac(AValue)*60);

If the fractional part contains seconds or even milli-seconds then you need a different calculation.

Secs := Round(Frac(AValue)*3600);

or

MSecs := Round(Frac(AValue)*3600000);

I don't see much point in using extended here. That type only exists distinct from double on x86 and in any case is slower than double due to poor alignment, and gives you nothing here that you cannot get from double.