Office中国论坛/Access中国论坛

标题: 请教:关于小数转换成分数[求助] [打印本页]

作者: che1188    时间: 2003-4-4 07:12
标题: 请教:关于小数转换成分数[求助]
在报表和窗体中如何让数字字段的小数显示成分数的格式,如:0.2显示成1/5。che1188@sohu.com[em11]
作者: zhaobeaut    时间: 2003-5-12 20:13
可以这样来解决啊: 我用VB做的,你可以参考一下
你应该知道怎么样把0.2转化成 2/10吧,这样不就好办了,求出分子和分母的最大公约数,然后进行除法不就可以了。程序如下:
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
End Sub

Private Sub Text1_Change()
Dim m As Integer
Dim n As Integer
Dim x As Integer
Dim y As Integer

If Text1.Text = "" Then
Exit Sub
End If

If Len(Text1.Text) < 3 Then
Exit Sub
End If

m = Val(10 ^ (Len(Text1.Text) - 2))
n = Val(Right(Text1.Text, Len(Text1.Text) - 2))
x = m
y = n

If y = 0 Then
Text2.Text = "0"
Exit Sub
End If

Do
z = x Mod y
x = y
y = z
Loop While z <> 0
Text2.Text = Str(n / x) & "/" & Str(m / x)
End Sub


现在只能处理不超过6位的小数

作者: secowu    时间: 2005-8-19 01:51
建议高手将它做成模块来用




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3