Someone know, how to do this with math only (without string conversions):
function Round_M(const aVal: Double): Double;
var
x: Double;
Str: string;
begin
if aVal < 0 then
x := aVal * 100 - 0.5
else
x := aVal * 100 + 0.5;
Str := FloatToStr(x);
if Pos(FormatSettings.DecimalSeparator, Str) > 0 then
Str := Copy(Str, 1, Pos(FormatSettings.DecimalSeparator, Str) - 1);
Result := StrToInt64(Str) * 0.01;
end;
It rounds to 0.01 and 0.005 always up. I try get same result with RoundTo
, Trunc
, Int
, SimpleRoundTo
... But f.ex 1.005 -> good rounded to 1.01 but 1.015 will be 1.02, and my solutions got 1.01. Same for 1.025, 1.035, 1.045, 1.055.