| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1378 人关注过本帖, 1 人收藏
标题:如何解决计算出来的数据的死舍五入问题呢?
取消只看楼主 加入收藏
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
结帖率:84.62%
收藏(1)
 问题点数:0 回复次数:6 
如何解决计算出来的数据的死舍五入问题呢?
请看代码:

Dim x2, y2, c2, ha2, hb2 As Single
Dim x3, y3, c3, ha3, hb3 As Single
Dim x4, y4, c4, ha4, hb4 As Single

ha = Val(Text1.Text)
hb = Val(Text2.Text)
c = Val(Text3.Text)
y = 1000 * (ha - hb)
x = 500 * Sqr(c * c - (ha - hb) * (ha - hb))
Text10.Text = Val(x)
Text11.Text = Val(y)
搜索更多相关主题的帖子: 数据 
2008-10-05 19:16
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 
我输出来的结果是带了8位小数,原来应该是整数的也变成了带8位的小数


是定义有误么?

DIM 。。。AS INTEGER么?

我试试。
2008-10-07 09:39
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 
我想结果为整数,通过四舍五入为整数
2008-10-07 09:40
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 
我用了代码如下:

Text10.Text = Int(Val(x))

取整数

但取的整数并不能解决四舍五入问题!

可能还涉及到取位涵数问题!

我用你的试试!
2008-10-07 11:30
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 
Private Sub Command1_Click()
Dim x, y, c, ha, hb As Single
Dim x2, y2, c2, ha2, hb2 As Integer
Dim x3, y3, c3, ha3, hb3 As Integer
Dim x4, y4, c4, ha4, hb4 As Integer

ha = Val(Text1.Text)
hb = Val(Text2.Text)
c = Val(Text3.Text)
y = 1000 * (ha - hb)
x = 500 * Sqr(c * c - (ha - hb) * (ha - hb))
Text10.Text = Int(Val(x))
Text11.Text = Int(Val(y))

If Text4.Text <> "" Then
ha2 = Val(Text2.Text)
hb2 = Val(Text4.Text)
c2 = Val(Text5.Text)
y2 = 1000 * (ha2 - hb2)
x2 = 500 * Sqr(c2 * c2 - (ha2 - hb2) * (ha2 - hb2))
Text12.Text = Val(x2)
Text13.Text = Val(y2)
Else
Text12.Text = ""
Text13.Text = ""
End If

If Text6.Text <> "" Then

ha3 = Val(Text4.Text)
hb3 = Val(Text6.Text)
c3 = Val(Text7.Text)
y3 = 1000 * (ha3 - hb3)
x3 = 500 * Sqr(c3 * c3 - (ha3 - hb3) * (ha3 - hb3))
Text14.Text = Val(x3)
Text15.Text = Val(y3)
Else
Text14.Text = ""
Text15.Text = ""
End If

If Text8.Text <> "" Then
ha4 = Val(Text6.Text)
hb4 = Val(Text8.Text)
c4 = Val(Text9.Text)
y4 = 1000 * (ha4 - hb4)
x4 = 500 * Sqr(c4 * c4 - (ha4 - hb4) * (ha4 - hb4))
Text16.Text = Val(x4)
Text17.Text = Val(y4)
Else
Text16.Text = ""
Text17.Text = ""
End If
End Sub

Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""

Text7.Text = ""
Text8.Text = ""
Text9.Text = ""
Text10.Text = ""
Text11.Text = ""
Text12.Text = ""
Text13.Text = ""
Text14.Text = ""
Text15.Text = ""
Text16.Text = ""
Text17.Text = ""

End Sub

Private Sub Command3_Click()
End
End Sub


你自己试试
2008-10-07 11:32
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 
我是这样解决的,代码如下:

If Int(Val(x) + 0.5) > Int(Val(x)) Then
Text10.Text = Int(Val(x) + 1)
Else: Text10.Text = Int(Val(x))
End If
Text11.Text = Int(Val(y) + 0.01)


If Text4.Text <> "" Then
ha2 = Val(Text2.Text)
hb2 = Val(Text4.Text)
c2 = Val(Text5.Text)
y2 = 1000 * (ha2 - hb2)
x2 = 500 * Sqr(c2 * c2 - (ha2 - hb2) * (ha2 - hb2))
If Int(Val(x2) + 0.5) > Int(Val(x2)) Then
Text12.Text = Int(Val(x2) + 1)
Else: Text12.Text = Int(Val(x2))
End If

经过测试,是正确的!

还请指教!

谢谢!
2008-10-10 11:11
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 
If Int(Val(x) + 0.5) > Int(Val(x)) Then
Text10.Text = Int(Val(x) + 1)
Else: Text10.Text = Int(Val(x))
End If
Text11.Text = Int(Val(y) + 0.01)

这是解决四舍五入问题的;

If Int(Val(x2) + 0.5) > Int(Val(x2)) Then
Text12.Text = Int(Val(x2) + 1)
Else: Text12.Text = Int(Val(x2))

这是解决取整数问题的(非四舍五入问题)的。
2008-10-10 11:14
快速回复:如何解决计算出来的数据的死舍五入问题呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016123 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved