Advertisement
WarPie90

TruncInt & RoundEx

Dec 20th, 2013
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.83 KB | None | 0 0
  1. {*
  2.  Truncates an integer.
  3. *}
  4. function TruncInt(x, n: Integer): Integer;
  5. var
  6.   i,digits,v: Integer;
  7.   m:boolean;
  8. begin
  9.   if x < 0 then begin
  10.     x := -x;
  11.     m := True;
  12.   end;
  13.   v := 1;
  14.   while v < x do
  15.   begin
  16.     v := v * 10;
  17.     inc(digits);
  18.     if digits >= 9 then Break;
  19.   end;
  20.   n := Abs(Min(n,digits) - digits);
  21.   for i:=0 to n-1 do
  22.     x := (x div 10);
  23.  
  24.   if not(m) then Result := x
  25.   else Result := -x;
  26. end;
  27.  
  28.  
  29. {*
  30.  Same functionality as Round(x,n) in Delphi
  31. *}
  32. function RoundEx(x: Extended; N: byte): extended;
  33. var
  34.   i, j : integer;
  35. begin
  36.   if N > 10 then N := 10;
  37.   if N > 0 then
  38.   begin
  39.     i := 1;
  40.     for j:=1 to N do
  41.       i := i * 10;
  42.     result := Round(x * i) / i;
  43.   end else
  44.     result := Round(x);
  45. end;
  46.  
  47.  
  48. begin
  49.   WriteLn(TruncInt(314159265, 5));
  50.   WriteLn(RoundEx(3.14159265, 4));
  51. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement