Why does Delphi's RoundTo method behave differently?
I noticed that the RoundTo () function in both Delphi 7 and XE6 behaves differently with some numbers, and none of them refer to the bankers method as they said in the documentation. I am passing the following RoundTo number numbers (val, -4) in Delphi 7 and XE6 and they return different results for some numbers, can you please explain why?
Value Dephi7 XE6
69.72505 69.7250 69.7250
69.72515 69.7251 69.7251
69.72525 69.7252 69.7253 <-- diff
69.72535 69.7254 69.7254
69.72545 69.7254 69.7254
69.72555 69.7255 69.7255
69.72565 69.7256 69.7257 <-- diff
69.72575 69.7258 69.7258
69.72585 69.7258 69.7258
69.72595 69.7259 69.7259
This is the delphi code I used to generate the numbers above. I used the code to compile in Delphi 7 and XE6 and got a different result.
procedure TForm1.Button1Click(Sender: TObject);
const
valueList : Array[0..9] of double =
( 69.72505,
69.72515,
69.72525,
69.72535,
69.72545,
69.72555,
69.72565,
69.72575,
69.72585,
69.72595
);
var
d : double;
i : Integer;
begin
Memo1.Clear;
for i := Low(valueList) to High(valueList) do
Memo1.Lines.Add(FloatToStr(valueList[i]) + ', ' + FloatToStr(RoundTo(valueList[i], -4)));
However, if I do it in C # using Round (val, 4), this is what I get, which is consistent with the bankers' method:
Value Round
69.72505 69.7250
69.72515 69.7252
69.72525 69.7252
69.72535 69.7254
69.72545 69.7254
69.72555 69.7256
69.72565 69.7256
69.72575 69.7258
69.72585 69.7258
69.72595 69.7260
One more thing, with the following code, which is an example from the delphi documentation:
ShowMessage(FloatToStr(RoundTo(1.245, -2)));
Delphi 7 gives me 1.25 and XE6 gives me 1.24, which is contrary to their docs. (delphi 7 doco says 1.24 but XE6 doco says 1.25).
Could you explain why?
source to share
No one has answered this question yet
See similar questions:
or similar: