Mirror

Variety of Floating point functions (Views: 100)


Problem/Question/Abstract:

Variety of Floating point functions

Answer:

// FloatDecimals

function FloatDecimals(Value, decimals: extended): extended;
var
  Factor: extended;
begin
  Factor := Power(10, Decimals);
  Value := Value * Factor;
  Value := Round(Value);
  Value := Value / Factor;
  Result := Value;
end;

// FloatRound ( Same as FloatDecimals but more aqurate )

function FloatRound(Value, Digits: extended): extended;
var
  Factor: extended;
begin
  Factor := Power(10, Digits);
  Result := (Value * Factor) + 0.5;
  Result := Trunc(Result) / Factor;
end;

// FloatCompare

function FloatCompare(Value1, Value2: Extended): Boolean;
begin
  Result := False;
  if abs(Value1 - Value2) < 0.00001 then
    Result := True;
end;

// FloatLessEqual

function FloatLessEqual(Value1, Value2: extended): Boolean;
begin
  Result := False;
  if (abs(Value1 - Value2) < 0.00001) or (Value1 < Value2) then
    Result := True;
end;

// FloatGreateEqual

function FloatGreaterEqual(Value1, Value2: extended): Boolean;
begin
  Result := False;
  if (abs(Value1 - Value2) < 0.00001) or (Value1 > Value2) then
    Result := True;
end;

// FloatStr (Format a extended value towards a string with 2 decimals )

function FloatStr(Value: extended): string;
begin
  Result := FloatToStrF(Value, ffFixed, 18, 2);
end;

// FloatStr ( Format a extended value towards a string with specified decimals )

function FloatStr(Value: extended; Digits: Byte): string;
begin
  Result := FloatToStrF(Value, ffFixed, 18, Digits);
end;

<< Back to main page