设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1046|回复: 5
打印 上一主题 下一主题

[与其它组件] 关于四舍五入的问题

[复制链接]
跳转到指定楼层
1#
发表于 2006-5-29 16:58:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
关于四舍五入的问题

我在网上找到一个关于四舍五入的代码,但由于本人太菜不知道怎么使用,不知那位能帮帮我

Public Function cmcrnd(anumber As Double) As Double
anumber = Int(anumber * 10 ^ 6) / 10 ^ 6
Dim a As Double
Dim a1, n As Byte
Dim t As String
n = 0
a = anumber
t = Mid(Trim(Str(FORMatNumber(a - Int(a), 4, , , vbUseDefault))), 2)

For a1 = 1 To 4
If Mid(t, a1, 1) <> "0" Then
Exit For
Else: n = n + 1
End If
Next </P><>If Eval(Right(Str(Int(a * 1000 * 10 ^ n)), 1)) > 5 And Eval(Right(Str(Int(a * 100000)), 2)) > 0 Then
a = Int(a * 100 * 10 ^ n + 1) / (100 * 10 ^ n)
Else:
If Eval(Right(Str(Int(a * 1000 * 10 ^ n)), 1)) = 5 Then
If Eval(Right(Str(Int(a * 100 * 10 ^ n)), 1)) Mod 2 = 0 Then
a = Int(a * 100 * 10 ^ n) / (100 * 10 ^ n)
Else: a = Int(a * 100 * 10 ^ n + 1) / (100 * 10 ^ n)
End If
Else: a = Int(a * 100 * 10 ^ n) / (100 * 10 ^ n)
End If
End If

Select Case no
Case 1
a = FORMatNumber(a, 3)
Case 2
a = FORMatNumber(a, 4)
Case 3
a = FORMatNumber(a, 5)
End Select
cmcrnd = a
End Function
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2006-5-29 17:01:00 | 只看该作者
保存到模块里,象普通函数一样调用
3#
 楼主| 发表于 2006-5-29 17:02:00 | 只看该作者
我是这样做得

但我不知道怎么调用
4#
发表于 2006-5-29 17:15:00 | 只看该作者
你提供的代码有错误,可参照一下这个例子

http://www.office-cn.net/vvb/dispbbs.asp?BoardID=150&replyID=96879&id=19438&skin=0
5#
 楼主| 发表于 2006-5-29 17:28:00 | 只看该作者



专用与计量认证的数据处理。

规则:1 保留小数点后两位有效数字(以小数点后,第一个非零数算起两位数字)
   2 四舍六入五奇偶:“五奇偶”是指第三位有效数为5时,要看第二位的奇偶属性
    奇数进,偶数舍
举例:11.00155 ----> 11.0016
   11.00165 ----> 11.0016
   11.135  ----> 11.14
   11.500  ----> 11.50
   11.550  ----> 11.55
   11.56001 ----> 11.56  *只要有非零尾数,无论奇偶都要进位
----------------------------------
Public Function cmcrnd(anumber As Double) As Double
anumber = Int(anumber * 10 ^ 6) / 10 ^ 6
Dim a As Double
Dim a1, n As Byte
Dim t As String
n = 0
a = anumber
t = Mid(Trim(Str(FORMatNumber(a - Int(a), 4, , , vbUseDefault))), 2)

For a1 = 1 To 4
If Mid(t, a1, 1) <> "0" Then
Exit For
Else: n = n + 1
End If
Next

If Eval(Right(Str(Int(a * 1000 * 10 ^ n)), 1)) > 5 And Eval(Right(Str(Int(a * 100000)), 2)) > 0 Then
a = Int(a * 100 * 10 ^ n + 1) / (100 * 10 ^ n)
Else:
If Eval(Right(Str(Int(a * 1000 * 10 ^ n)), 1)) = 5 Then
If Eval(Right(Str(Int(a * 100 * 10 ^ n)), 1)) Mod 2 = 0 Then
a = Int(a * 100 * 10 ^ n) / (100 * 10 ^ n)
Else: a = Int(a * 100 * 10 ^ n + 1) / (100 * 10 ^ n)
End If
Else: a = Int(a * 100 * 10 ^ n) / (100 * 10 ^ n)
End If
End If

Select Case no
Case 1
a = FORMatNumber(a, 3)
Case 2
a = FORMatNumber(a, 4)
Case 3
a = FORMatNumber(a, 5)
End Select
cmcrnd = a
End Function



能告诉我这段代码错在那里吗

[此贴子已经被作者于2006-5-29 9:32:59编辑过]

6#
发表于 2006-6-7 07:57:00 | 只看该作者
应该是,要编译代码,才能发现错误

Public Function cmcrnd(anumber As Double) As Double
anumber = Int(anumber * 10 ^ 6) / 10 ^ 6
Dim a As Double
Dim a1, n As Byte
Dim t As String
n = 0
a = anumber
t = Mid(Trim(Str(FormatNumber(a - Int(a), 4, , , vbUseDefault))), 2)

For a1 = 1 To 4
If Mid(t, a1, 1) <> "0" Then
Exit For
Else: n = n + 1
End If
Next

If Eval(Right(Str(Int(a * 1000 * 10 ^ n)), 1)) > 5 And Eval(Right(Str(Int(a * 100000)), 2)) > 0 Then
a = Int(a * 100 * 10 ^ n + 1) / (100 * 10 ^ n)
Else:
If Eval(Right(Str(Int(a * 1000 * 10 ^ n)), 1)) = 5 Then
If Eval(Right(Str(Int(a * 100 * 10 ^ n)), 1)) Mod 2 = 0 Then
a = Int(a * 100 * 10 ^ n) / (100 * 10 ^ n)
Else: a = Int(a * 100 * 10 ^ n + 1) / (100 * 10 ^ n)
End If
Else: a = Int(a * 100 * 10 ^ n) / (100 * 10 ^ n)
End If
End If

Select Case n
Case 1
a = FormatNumber(a, 3)
Case 2
a = FormatNumber(a, 4)
Case 3
a = FormatNumber(a, 5)
End Select
cmcrnd = a
End Function
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-11-15 20:34 , Processed in 0.083233 second(s), 29 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表