Vay ... böylece beklenen değer almayabilir, her zaman küçük yuvarlama hatalarının bir şans var! çocuklar, çok basit şeyleri nasıl karmaşıklaştırıyorsunuz ... ayrıca çoğunuz en yakın 1/100 saniyeye kadar yuvarlama seçeneğinizi kaybedersiniz ...
Bu, çok daha basit ve aynı zamanda milisenaniye yuvarlanabilir parçalar:
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;
sadece bu ortak ya da değil çok yaygın örnekleri ile test edebilirsiniz: Bu benim gibi insanlara yardım
// 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))
)
);
Hope, o isteği Yuvarlamada 1/100, 1/25 veya 1/10 saniye.
Ceil ve Kat gibi
Cevabımdaki sorun neydi? –
Hiçbir şey, gerçekten, sadece Gabr'ın çözümünü test etmek için geldim. Ayrıca, interval türü ve boyutu için tek bir parametre önerisi, aynı şey için İKİ parametreli bir çözümden daha zarifti. Bence en azından. –
Bu çok kullanışlı bir kod olup, saatlerce veya dakikalarca defalarca artırırsanız datetime 'drift' eğilimindedir. Sıkı bir zaman dizisi için çalışıyorsanız, işleri bozabilir. Örneğinizle ilgili birkaç niggli olsa da, varsayılan değer benim için işe yaramadı, ayrıca '(vTimeSec/SecsPerDay)' çıkışından sonra bir hata olduğunu düşünüyorum, orada olmamalı. Düzeltmeler ve yorumlar ile kodum: – SolarBrian