Round2
The Round2 function returns the specified aValue rounded to the number of decimal places given by aDigit.
This function performs traditional biased rounding, where the ending decimal value of 1-5 is rounded down, and 6-9 is rounded up.
Where aDigit is 0, the function applies the same rules, but just returns the integer –
Round2(4.2,0) is 4.
Round2(4.5,0) is 4.
Round2(4.8,0) is 5.
For a function that always rounds 0.5 up, refer to Round2Up.
Declaration: Function Round2(aValue : Extended; aDigit : integer) : Extended
Examples are as below.
Procedure
OnMapEvent(
var
Value:variant);
begin
Value:= Round2(
745.454
,
2
);
// result is 745.45
Value:= Round2(
745.455
,
2
);
// result is 745.45
Value:= Round2(
745.456
,
2
);
// result is 745.46
Value:= Round2(
745.4558
,
3
);
// result is 745.456
Value:= Round2(
745.4553
,
3
);
// result is 745.455
Value:= Round2(
745.554
,
2
);
// result is 745.55
Value:= Round2(
745.555
,
2
);
// result is 745.55
Value:= Round2(
745.556
,
2
);
// result is 745.56
Value:= Round2(
745.5558
,
3
);
// result is 745.556
Value:= Round2(
745.5553
,
3
);
// result is 745.555
LogInfo(FloatToStr(Round2(
745.5553
,
0
)));
// result is 746
LogInfo(FloatToStr(Round2(
745.5453
,
0
)));
// result is 745
LogInfo(FloatToStr(Round2(
745.4553
,
0
)));
// result is 745
LogInfo(FloatToStr(Round2(
745.5
,
0
)));
// result is 745
LogInfo(FloatToStr(Round2(
745.6
,
0
)));
// result is 746
LogInfo(FloatToStr(Round2(
666.777
,
2
)));
// result is 666.78
end
;