关于数值取值头痛的问题,请大侠指点

orchidbing 2004-04-12 12:30:06
由于工作需要,遇到很复杂数值取值问题:
保留三位有效数字,如果小于1时小数最多三位。
比如:222、2.11、12.1、0.001、0.111、0.100
采用四舍六入法,如果等于五的时候,前面一位如果是奇数那么5进一位,如果是
偶数,那么5后面是0不进,如果5后面不是0那么进一位。
比如:2.8150取值为 2.82
2.8151、2.8152、2.8153取值为2.82
2.8751、2.8752、2.8758取值为2.88
2.8351、2.8352、2.8353取值为2.84
2.8050取值为 2.80
2.8250取值为 2.82
2.8651取值为 2.87
...全文
109 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
MAGXJ500T 2004-04-19
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/TopicView1.asp?id=2980930
orchidbing 2004-04-19
  • 打赏
  • 举报
回复
在Delphi中使用Round函数得到的答案有时与我们所预期的会不太一样:
采用的是四舍六入五留双。即当舍或入位大于或小于五时按四舍五入来处理,而当舍或入位等于五时,就要看前面一位是什么,根据奇进偶不进,它总是返回一个偶数值。
示例 结果
i:= Round(11.5) 12
i:= Round(10.5) 10

这种Round其实是按照银行家算法,统计学上一般都用这种算法,比传统的"四舍五入"要科学。
如果要使用传统的"四舍五入"方法.
早知道就……
请问lmjzw(电脑世界)里面用的right是什么单元,lenth是length吧
lywho 2004-04-12
  • 打赏
  • 举报
回复
用round不是直接可以做吗?
orchidbing 2004-04-12
  • 打赏
  • 举报
回复
忘了说明一下输入的结果一般不会大于10
orchidbing 2004-04-12
  • 打赏
  • 举报
回复
to: aiirii(ari-爱的眼睛)我测试了你的,这个只能处理大于0的,如果小于0就不行了,比如0.05651之类的。小于1的时候结果要取三位小数。
to: lmjzw(电脑世界) 测试也通过不了,能不能帮我再改进改进,麻烦了。
拜托大家帮忙了,这个解决不了我就没法继续编下去了。
leeky 2004-04-12
  • 打赏
  • 举报
回复
好像不难吧,只要你能描述清楚,你就能实现它。
lmjzw 2004-04-12
  • 打赏
  • 举报
回复
上面少了一End哈
不好意思!
lmjzw 2004-04-12
  • 打赏
  • 举报
回复
设变量X为要转换的数据:
var
x:integer;
str,str1,str2,str3:string;
begin
.....
str1:=inttostr(trunc(x)); //取得整数部分
str2:=inttostr(x-trunc(x)); //取得小数部分
if strtoint(str1)<1 then
begin
str:=floattostr(strtofloat(str1)+(round(strtofloat(str2)*1000)/1000.000))
end
else
begin
str:=str1;
str2:=floattostr(round(strtofloat(str2)*10000));
str3:=right(str2,1);
str2:=copy(str2,0,lenth(str2)-1);
if right(str2,1)='5' then
begin
if str3='0' then
str2:=inttostr(trunc(strtoint(str2)/10))
else
str2:=floattostr(round(strtofloat(str2)/10));
end
else
str2:=floattostr(round(strtofloat(str2/10.000)));
str:=floattostr(strtofloat(str)+strtofloat(str2));
end;

上面代码中Str中装的是所取的数据的字符类型!
aiirii 2004-04-12
  • 打赏
  • 举报
回复
uses Math;

var I, j:double;
begin
j := strToFloatdef(edit1.Text, 1.0);
i := RoundTo(j, -2);
label2.Caption := FloatToStr(i);
end;

我測試了下,
2.8050取值为 2.80 這個好象不行
duhailee 2004-04-12
  • 打赏
  • 举报
回复
按你的逻辑, 2.8651取值为 2.86 才是啊, 你怎么得的 :2.87
????????????

lzy6204 2004-04-12
  • 打赏
  • 举报
回复
有点难度,帮你关注一下:))

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧