数据类型转换表达问题(50分)

  • 主题发起人 主题发起人 轻松
  • 开始时间 开始时间

轻松

Unregistered / Unconfirmed
GUEST, unregistred user!
我有一个查询结果query1.fields[0].asstring,其值等于22.445,
希望将其生成一个浮点数,四舍五入,
再将其转换成string类型,赋给 lbl_sx.Caption
如何表达?

致谢!

lbl_sx.Caption:= query1.fields[0].asstring);
(注:要求使用类型转换函数)
另问:浮点数取整函数是什么?
 
inttostr(round(strtofloat(query1.fields[0].asstring)))
 
多谢lycok的帮助,但是未达目标:四舍五入,再转换成string
思路:
原值string 22.445
转成float 22.445
乘100 float 2244.5
加0.5 float 2245.0
取整 integer 2245
除以100 float 22.45
转成string string 22.45
OK

我未能做到的两步 整数与float的转换函数、取整函数
 
搞掂:
lbl_sx.Caption:=floattostr(round(strtofloat(query1.fields[0].asstring)*100+0.5)/100);
 
inttostr(round(strtofloat(query1.fields[0].asstring)))
aa:=inttostr(round(strtofloat(query1.fields[0].asstring)));
lbl_sx.Caption:= aa;
 
加0.5干啥?直接*100/100就得了,round本身就是四舍五入用的。
lbl_sx.Caption:=floattostr(round(strtofloat(query1.fields[0].asstring)*100)/100);

另有两个函数:ceil朝大取整,floor朝小取整
 
多谢。
不过,若使用HunterTeam的表达
lbl_sx.Caption:=floattostr(round(strtofloat(query1.fields[0].asstring)*100)/100);
当小数点第三位后为 5,即.??5时,不进位,若为.??500000001时,进位
所以,我作了一点改进,加了0.000001
lbl_sx.Caption:=floattostr(round(strtofloat(query1.fields[0].asstring)*100+0.000001)/100);
再次多谢各位的指导。
 
后退
顶部