|
|
导航: |
论坛 -> DELPHI技术
斑竹:liumazi,sephil |
|
作者: |
|
2004/2/15 16:28:23 |
标题: |
|
加入我的收藏 |
楼主: |
程序中计算出一个数,譬如:result:=2.5427842587l; 我想在在Label1输出时只显示整数部分以及小数的前三位,应该怎么处理?
----------------------------------------------
- |
作者: |
bios (阿贡) |
★☆☆☆☆ |
-
|
盒子中级会员 |
|
2004/2/15 16:58:51 |
1楼: |
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; Edit7: TEdit; 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.Button4Click(Sender: TObject); begin edit4.text:=myround(2.5427842587l,3); //2.543 end; end.
----------------------------------------------
|
作者: |
|
2004/2/15 17:12:42 |
2楼: |
大哥果然强啊,解释的也很清楚! 多谢了 !
我在“下载中心”http://www.2ccc.com/article.asp?articleid=475下载的“RemoteCXP 远程控制的源代码”,打开时提示的idFTCPClient,这是什么控件,谢谢!
----------------------------------------------
-
|
作者: |
nihg (研砉) |
★☆☆☆☆ |
-
|
普通会员 |
|
2004/2/15 17:13:47 |
2楼: |
在窗、体上加两个label控件 一个button ,以下是button的onlick事件
var s,a,b,c: variant; begin //设205 除以13 s:=205/13; //计算delphi的真值 b:=205 div 13; //取得整数部分 label1.caption:=floattostr( s) ;// label显示真值 a:=trunc (s/0.001); //显示整数与三位小数 c:=(a-b*1000)/ 1000 ;//取出小数位 label2.caption:=(floattostr(b+c)) //label2显示你要的东东
end;
----------------------------------------------
我爱呆儿飞
|
作者: |
bios (阿贡) |
★☆☆☆☆ |
-
|
盒子中级会员 |
|
2004/2/15 17:20:07 |
3楼: |
indy组的网络控件啊,找找吧,偶现在没空啊
----------------------------------------------
|
作者: |
|
2004/2/15 17:35:09 |
4楼: |
我觉得2楼的东东更简单实用,强烈要求顶一下
----------------------------------------------
-
|
作者: |
bios (阿贡) |
★☆☆☆☆ |
-
|
盒子中级会员 |
|
2004/2/15 17:37:16 |
5楼: |
:)呵呵自己写自己,自己完全有能力搞定,不一定看别人的!
----------------------------------------------
|
作者: |
|
2004/2/15 19:21:22 |
6楼: |
原来这么复杂啊
----------------------------------------------
-愿天下有情人终成眷属! http://www.pro-thinking.com/bbs/
|
作者: |
sephil (NAILY Soft) |
★☆☆☆☆ |
-
|
盒子中级会员 |
|
2004/2/15 19:41:25 |
7楼: |
Label1.Caption := FormatFloat('0.000', 2.5427842587l); 将显示 2.543 Label1.Caption := FormatFloat('0.000', 2.54); 将显示 2.540
Label1.Caption := FormatFloat('#.###', 2.5427842587l); 将显示 2.543 Label1.Caption := FormatFloat('#.###', 2.54); 将显示 2.54
----------------------------------------------
Copyright 2008 ? NAILY Soft Click here to redirect to my home Click here to redirect to my blog
|
|