Wow! ragazzi, come complicate troppo qualcosa di così semplice ... anche la maggior parte di voi perde l'opzione per arrotondare a 1/100 di secondo, ecc ...
Questo è molto più semplice e può anche arrotondare a milisecondi parti:
function RoundToNearest(TheDateTime,TheRoundStep:TDateTime):TdateTime;
begin
if 0=TheRoundStep
then begin // If round step is zero there is no round at all
RoundToNearest:=TheDateTime;
end
else begin // Just round to nearest multiple of TheRoundStep
RoundToNearest:=Round(TheDateTime/TheRoundStep)*TheRoundStep;
end;
end;
si può solo provare con questo comune o non così esempi comuni:
// Note: Scroll to bottom to see examples of round to 1/10 of a second, etc
// Round to nearest multiple of one hour and a half (round to 90'=1h30')
ShowMessage(FormatDateTime('hh:nn:ss.zzz'
,RoundToNearest(EncodeTime(15,31,37,156)
,EncodeTime(1,30,0,0))
)
);
// Round to nearest multiple of one hour and a quarter (round to 75'=1h15')
ShowMessage(FormatDateTime('hh:nn:ss.zzz'
,RoundToNearest(EncodeTime(15,31,37,156)
,EncodeTime(1,15,0,0))
)
);
// Round to nearest multiple of 60 minutes (round to hours)
ShowMessage(FormatDateTime('hh:nn:ss.zzz'
,RoundToNearest(EncodeTime(15,31,37,156)
,EncodeTime(1,0,0,0))
)
);
// Round to nearest multiple of 60 seconds (round to minutes)
ShowMessage(FormatDateTime('hh:nn:ss.zzz'
,RoundToNearest(EncodeTime(15,31,37,156)
,EncodeTime(0,1,0,0))
)
);
// Round to nearest multiple of second (round to seconds)
ShowMessage(FormatDateTime('hh:nn:ss.zzz'
,RoundToNearest(EncodeTime(15,31,37,156)
,EncodeTime(0,0,1,0))
)
);
// Round to nearest multiple of 1/100 seconds
ShowMessage(FormatDateTime('hh:nn:ss.zzz'
,RoundToNearest(EncodeTime(15,31,37,141)
,EncodeTime(0,0,0,100))
)
);
// Round to nearest multiple of 1/100 seconds
ShowMessage(FormatDateTime('hh:nn:ss.zzz'
,RoundToNearest(EncodeTime(15,31,37,156)
,EncodeTime(0,0,0,100))
)
);
// Round to nearest multiple of 1/10 seconds
ShowMessage(FormatDateTime('hh:nn:ss.zzz'
,RoundToNearest(EncodeTime(15,31,37,151)
,EncodeTime(0,0,0,10))
)
);
// Round to nearest multiple of 1/10 seconds
ShowMessage(FormatDateTime('hh:nn:ss.zzz'
,RoundToNearest(EncodeTime(15,31,37,156)
,EncodeTime(0,0,0,10))
)
);
Spero che questo aiuti le persone come me, che hanno bisogno di arrotondare al 1/100, 1/25 o 1/10 secondi.
che cosa era sbagliato con la mia risposta? –
Niente, davvero, mi è appena capitato di testare prima la soluzione di Gabr. Inoltre, il suo suggerimento di un singolo parametro per tipo di intervallo E dimensione era più elegante di una soluzione con DUE parametri per la stessa cosa. Secondo me almeno. –
Questo è un bit di codice molto utile, trovo che il datetime tende a 'drift' se lo si incrementa di ore o minuti molte volte. che può rovinare le cose se stai lavorando a una serie temporale rigorosa. Qualche pecca sul tuo esempio, anche se Svein, il valore predefinito non ha funzionato per me, anche il '(vTimeSec/SecsPerDay)' dopo l'uscita, penso che sia un errore, non dovrebbe esserci. Il mio codice con correzioni e commenti, è: – SolarBrian