1.Round(Four homes, six homes, five homes)
Function Description: rounds a real number. (Based on the banker's algorithm)
Example:
VaR
I, J: integer;
Begin
I: = round (1.5); // I equals 2
J: = round (2.5); // J equals 2
End;
InDelphiThe answer obtained by using the round function in is sometimes different from what we expected: four homes, six homes, five homes, and double homes. That is, when the number of homes or spaces is greater than or less than five, it is rounded to five.
When the house or entry is equal to five, it depends on what the first digit is. If it is not entered, it always returns an even value.
Example:
I: = round (11.5) // I equals 12
I: = round (10.5) // I equals 10
This kind of round is actually based on the Banker algorithm, which is generally used in statistics and is more scientific than the traditional "Rounding.
To use the traditional "Rounding" method, you can use the following function:
Function roundclassic (r: Real)
2.Trunc(Obtain the integer part of X)
For example:Trunc(-123.55) =-123, floor (123.55) = 123
3.Ceil(Obtain the smallest integer greater than or equal to X)
For example: Ceil (-123.55) =-123, Ceil (123.15) = 124
4.Floor(Obtain the largest integer smaller than or equal to X)
For example, floor (-123.55) =-124, floor (123.55) = 123
5. roundto (obtain the required decimal number)
To directly use the roundto function, you must add math to uses.
Roundto (1.245,-2); = 1.25
It should be noted that the old Delphi version of the round function uses four homes and six inputs. In every five, the first entry is an odd number, and the last entry is an even number, examples in the Delphi manual help are as follows:
Roundto (1234567, 3) 1234000
Roundto (1.234,-2) 1.23
Roundto (1.235,-2) 1.24
Roundto (1.245,-2) 1.24
But Delphi 7 is not like this anymore. It is rounded down directly: roundto (1.245,-2) = 1.25
Note: floor and ceil are functions in math unit. before using them, uses math is required.
procedure TForm1.btn1Click(Sender: TObject);var sedt1: string;begin sedt1 := edt1.Text; edt2.Text := FloatToStr(RoundFloat(StrToFloat(sedt1), 2));end;procedure TForm1.btn2Click(Sender: TObject);var sedt1: string;begin sedt1 := edt1.Text; edt2.Text := FloatToStr(RoundFloatZero(StrToFloat(sedt1), 2));end;procedure TForm1.btn3Click(Sender: TObject);var sedt1: string;begin sedt1 := edt1.Text; edt2.Text := FloatToStr(Round(StrToFloat(sedt1)));end;procedure TForm1.btn4Click(Sender: TObject);var sedt1: string;begin sedt1 := edt1.Text; edt2.Text := FloatToStr(trunc(StrToFloat(sedt1)));end;procedure TForm1.btn5Click(Sender: TObject);var sedt1: string;begin sedt1 := edt1.Text; edt2.Text := FloatToStr(ceil(StrToFloat(sedt1)));end;procedure TForm1.btn6Click(Sender: TObject);var sedt1: string;begin sedt1 := edt1.Text; edt2.Text := FloatToStr(floor(StrToFloat(sedt1)));end;procedure TForm1.btn7Click(Sender: TObject);var sedt1: string;begin sedt1 := edt1.Text; edt2.Text := FloatToStr(RoundTo(StrToFloat(sedt1), -2));end;{ TForm1 }function TForm1.RoundFloat(AFloat: Double; ADigits: Integer): Double;var sFormat: string; eAvoidError: extended;begin sFormat := ‘#.‘ + StringOfChar(‘0‘, ADigits); eAvoidError := StrToFloat(FloatToStr(AFloat)); Result := StrToFloat(FormatFloat(sFormat, eAvoidError));end;function TForm1.RoundFloatZero(AFloat: Double; ADigits: Integer): Double;var sFormat: string; eAvoidError: extended;begin sFormat := ‘0.‘ + StringOfChar(‘0‘, ADigits); eAvoidError := StrToFloat(FloatToStr(AFloat)); Result := StrToFloat(FormatFloat(sFormat, eAvoidError));end;