Hi.

There is a form with 2 buttons.

Button1 OnClick event looks like this:

procedure TForm1.Button1Click(Sender: TObject);

function RoundedAmount(R: double; D: Longint): Double;

function SymbolOf(Nbr: Double): Double;

begin

if Nbr < 0 then

Result := -1

else

Result := 1;

end;

function NbrExponentOf(Nbr: Double; Exponent: Integer): Double;

begin

Result := 1;

while Exponent < 0 do

begin

Result := Result / Nbr;

Exponent := Exponent + 1;

end;

while Exponent > 0 do

begin

Result := Result * Nbr;

Exponent := Exponent - 1;

end;

end;

var

P: double;

X: double;

dNbrExp: double;

begin

P := SymbolOf(R) * 0.5;

dNbrExp := NbrExponentOf(10, D);

X := R * dNbrExp + P;

Result := int(X) / dNbrExp;

end;

begin

showmessage(FloatToStr(RoundedAmount(0.105, 2)));

end;

And here is Button2.OnClick event:

procedure TForm1.Button2Click(Sender: TObject);

function RoundedAmount(R: double; D: Longint): Double;

function SymbolOf(Nbr: Double): Double;

begin

if Nbr < 0 then

Result := -1

else

Result := 1;

end;

function NbrExponentOf(Nbr: Double; Exponent: Integer): Double;

begin

Result := 1;

while Exponent < 0 do

begin

Result := Result / Nbr;

Exponent := Exponent + 1;

end;

while Exponent > 0 do

begin

Result := Result * Nbr;

Exponent := Exponent - 1;

end;

end;

var

P: double;

begin

P := SymbolOf(R) * 0.5;

Result := Int(R * NbrExponentOf(10, D) + P) / NbrExponentOf(10, D);

end;

begin

showmessage(FloatToStr(RoundedAmount(0.105, 2)));

end;

The only difference is the inline "RoundedAmount" which is slightly different between Button1 and Button2. "RoundedAmount" is supposed to round an amount of money (No I CANNOT use any Delphi's Math functions like RoundTo. The point isn't there anyways). Here are the only differences between the 2 functions:

With Button2, I calculate RESULT on a single line:

P := SymbolOf(R) * 0.5;

Result := Int(R * NbrExponentOf(10, D) + P) / NbrExponentOf(10, D);

With Button1, I calculate RESULT using multiple variables so it can stand on multiple lines:

P := SymbolOf(R) * 0.5;

dNbrExp := NbrExponentOf(10, D);

X := R * dNbrExp + P;

Result := int(X) / dNbrExp;

At first look, both functions should give the same result, right ?. And that is my problem: IT DOES NOT!!

When I hit Button1 I get 0.11 and with Button2 I get 0.1.

My question is simple: why is that ? How can I fix this ?

http://www.delphibasics.co.uk

David