Advertisement
WarPie90

Rounding

Dec 20th, 2013
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.96 KB | None | 0 0
  1. {*
  2.  Same functionality as Round(x,n) in Delphi
  3. *}
  4. function RoundEx(x: Extended; N: byte): extended;
  5. var
  6.   i, j : integer;
  7. begin
  8.   if N > 10 then N := 10;
  9.   if N > 0 then
  10.   begin
  11.     i := 1;
  12.     for j:=1 to N do
  13.       i := i * 10;
  14.     result := Round(x * i) / i;
  15.   end else
  16.     result := Round(x);
  17. end;
  18.  
  19.  
  20. function TruncEx(x: Extended; N: byte): extended;
  21. var
  22.   i, j : integer;
  23. begin
  24.   if N > 10 then N := 10;
  25.   if N > 0 then
  26.   begin
  27.     i := 1;
  28.     for j:=1 to N do
  29.       i := i * 10;
  30.     result := Trunc(x * i) / i;
  31.   end else
  32.     result := Trunc(x);
  33. end;
  34.  
  35.  
  36. function CeilEx(x: Extended; N: byte): extended;
  37. var
  38.   i, j : integer;
  39. begin
  40.   if N > 10 then N := 10;
  41.   if N > 0 then
  42.   begin
  43.     i := 1;
  44.     for j:=1 to N do
  45.       i := i * 10;
  46.     result := Ceil(x * i) / i;
  47.   end else
  48.     result := Ceil(x);
  49. end;
  50.  
  51.  
  52. begin
  53.   WriteLn(RoundEx(3.14159265, 4));
  54.   WriteLn(TruncEx(3.14159265, 4));
  55.   WriteLn(CeilEx(3.14159265, 4));
  56. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement