Delphi provides standard commands to round and truncate decimal numbers. For Example:
Round(12.75) = 13
Trunc(12.75) = 12
Int(12.75) = 12
Frac(12.75) = 0.75
What about if we want to use a specific number of decimal digits? For example, we want to use 2 decimal digits and 1,2345 to be rounded to 1,24 or 3 decimal digits and have as result 1,235.
Find below the nkRound commands to implement this:
Function nkRound(x: Real; DecimalDigits: Integer): Real;
var
i, f: integer;
r: Real;
begin
f:=1;
for i:=1 to DecimalDigits do
f:=f*10;
r:=x*f;
r:=Round(r);
r:=r/f;
Result:=r;
end;
No comments:
Post a Comment
Please leave you comment to improve our services and support