呼如林
代码如下:***************************************************unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Edit1: TEdit;Edit2: TEdit;Label1: TLabel;Label2: TLabel;Button1: TButton;Label3: TLabel;Edit3: TEdit;Label4: TLabel;procedure Button1Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end; varForm1: TForm1; implementation {$R *.dfm}function MakeStr(Value:Integer):string;vari: Integer;str: string;beginstr := '';for i := 1 to Value - 1 dobeginstr := str + '0';end;Result := '0.' + str + '1';end; function MyFun(source:Double;cut:Integer):string;varp: Integer;Sstr, Dstr: string;beginSstr := FloatToStr(source);p := Pos('.', Sstr);p := p + cut + 1;if Length(Sstr) < p thenSstr := Sstr + '0'; if p > 0 thenbegin//末位为 4 舍if StrToInt(Sstr[p]) <= 4 thenbegin//保留整数部分if cut = 0 thenDstr := Copy(Sstr,1,p-2)elseDstr := Copy(Sstr,1,p-1);Result := Dstr;end//末位为 5 判断else if StrToInt(Sstr[p]) = 5 thenbegin//5后非0 进 1if Length(Sstr) < p + 1 thenSstr := Sstr + '0'; if StrToInt(Sstr[p+1]) <> 0 thenbegin//保留整数部分if cut = 0 thenDstr := IntToStr(StrtoInt(Copy(Sstr,1,p-2)) + 1)elseDstr := FloatToStr(StrToFloat(Copy(Sstr,1,p-1)) + StrToFloat(MakeStr(cut)));Result := Dstr;endelse begin//保留整数部分if cut = 0 thenbegin//5前为偶 舍if (StrToInt(Sstr[p-2]) mod 2) = 0 thenbeginDstr := Copy(Sstr,1,p-2);Result := Dstr;end//5前为奇 进else beginDstr := IntToStr(StrtoInt(Copy(Sstr,1,p-2)) + 1);Result := Dstr;end;endelsebegin//5前为偶 舍if (StrToInt(Sstr[p-1]) mod 2) = 0 thenbeginDstr := Copy(Sstr,1,p-1);Result := Dstr;end//5前为奇 进else beginDstr := FloatToStr(StrToFloat(Copy(Sstr,1,p-1)) + StrToFloat(MakeStr(cut)));Result := Dstr;end;end;end;end//末位为 6 进else if StrToInt(Sstr[p]) >= 6 thenbegin//保留整数部分if cut = 0 thenDstr := IntToStr(StrtoInt(Copy(Sstr,1,p-2)) + 1)else//对应进位计算Dstr := FloatToStr(StrToFloat(Copy(Sstr,1,p-1)) + StrToFloat(MakeStr(cut)));Result := Dstr;end;end;end; procedure TForm1.Button1Click(Sender: TObject);varcut: integer;begincut := StrToInt(Trim(Edit3.Text));Edit2.Text := MyFun(StrToFloat(Edit1.Text ),cut);end; end.*************************************************实现效果: