|
|
导航: |
论坛 -> DELPHI技术
斑竹:liumazi,sephil |
|
作者: |
|
2003/12/24 10:54:03 |
标题: |
请教DELPHI7中FLOAT型数据四舍五入的问题! |
浏览:1795 |
|
加入我的收藏 |
楼主: |
请问在DELPHI7中如何实现下面的四舍五入:
10.06 变成11.1。 我试了round 不行。
----------------------------------------------
- |
作者: |
|
2003/12/24 13:06:37 |
1楼: |
请大虾指教啊!
----------------------------------------------
-
|
作者: |
|
2003/12/24 13:55:38 |
2楼: |
用formatfloat函数就行
----------------------------------------------
为啥子就是俺不懂呢!!!
|
作者: |
zxdcj (月下舞者) |
★☆☆☆☆ |
-
|
盒子活跃会员 |
|
2003/12/24 14:10:42 |
3楼: |
formatfloat('0.0',10.06);
----------------------------------------------
-
|
作者: |
bios (阿贡) |
★☆☆☆☆ |
-
|
盒子中级会员 |
|
2003/12/24 14:29:40 |
4楼: |
偶 经常自己写的: unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Button1: TButton; Button2: TButton; Edit3: TEdit; Button3: TButton; Edit4: TEdit; Edit5: TEdit; Edit6: TEdit; Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.dfm} function myround(yuan:Extended;pp:LOngint):string; //自己写最好! //yuan:原浮点数,PP保留 小数点后第几位 var i,p,l,m,l2:Longint; s:string; // 原浮点数 字符 比如11.06 sq:string; // 小数点前 sh:string;//小数点后 begin if yuan=0 then exit;// 原浮点数 0 if pp<0 then exit; //非法小数点后第几位 s:=floattostr(yuan); p:=pos('.',s); //小数点位置 sq:=copy(s,1,p-1); sh:=copy(s,p+1,length(s)); l:=length(sh); l2:=length(sq); if pp>l then begin//0 result:=s; exit;//比如 11。06 要保留到 小数点后第3位显然 不合理 end;// if pp=l then //比如 11。06 要保留到 小数点后第2位不用处理 直接返回 begin//1 Result:=s; exit; end;//1 if pp<l then //比如 11。06 要保留到 小数点后第1位 ,。。。 begin//2 m:=strtoint(sh[pp+1]); if m>=5 then if pp>=1 then //保留到 小数点后第1,2。。。位 begin//3 delete(sh,pp+1,1); sh[pp]:=chr(ord(sh[pp])+1); Result:=sq+'.'+sh; end//3 else //保留到 小数点后第0位 begin//4
sq[l2]:=chr(ord(sq[l2])+1); Result:=sq; end;//4
exit; end;//1 end;
procedure TForm1.Button1Click(Sender: TObject); begin edit1.text:=inttostr(round(3.5));//4 end;
procedure TForm1.Button2Click(Sender: TObject); begin edit2.text:=inttostr(round(11.06)); //11 end;
procedure TForm1.Button3Click(Sender: TObject); begin edit3.text:=inttostr(round(2.5)); //2 end;
procedure TForm1.Button4Click(Sender: TObject); begin edit4.text:=myround(3.5,0); //4 edit5.text:=myround(11.06,1); //11.1 edit6.text:=myround(2.5,0); //3 end;
end.
----------------------------------------------
|
|