VB中将小数化成分数
谁能告诉我,对于一个小数位数不确定的小数而言,怎么将它化成分数,比如说一个变量为a,它是单精度型的,然后我要让它用分数显示的话,怎么实现?...
谁能告诉我,对于一个小数位数不确定的小数而言,怎么将它化成分数,比如说一个变量为a,它是单精度型的,然后我要让它用分数显示的话,怎么实现?
展开
3个回答
展开全部
分2步:
1、复制下面函数到模块中
Function fs(xs As Single) As String
Dim fz, fm, fz1, fm1, t, zs
Dim i As Integer
t = CStr(xs)
i = InStr(t, ".")
If i = 0 Then
fs = t
Exit Function
End If
zs = Val(Left(t, i - 1)) '整数部分
fm = Val("1" & String(Len(Mid(t, i + 1)), "0")) '分母
fz = zs * fm + Val(Mid(t, i + 1)) '分子
fz1 = fz: fm1 = fm
Do
If fz1 < fm1 Then fm1 = fm1 - fz1
If fz1 > fm1 Then fz1 = fz1 - fm1
Loop Until fz1 = fm1 '约分
fz = fz / fz1
fm = fm / fm1
fs = fz & "/" & fm '此处的FZ为约分后的分子,FM为分母
End Function
2、函数调用举例:
dim a as single
a=0.125
msgbox fs(a)
1、复制下面函数到模块中
Function fs(xs As Single) As String
Dim fz, fm, fz1, fm1, t, zs
Dim i As Integer
t = CStr(xs)
i = InStr(t, ".")
If i = 0 Then
fs = t
Exit Function
End If
zs = Val(Left(t, i - 1)) '整数部分
fm = Val("1" & String(Len(Mid(t, i + 1)), "0")) '分母
fz = zs * fm + Val(Mid(t, i + 1)) '分子
fz1 = fz: fm1 = fm
Do
If fz1 < fm1 Then fm1 = fm1 - fz1
If fz1 > fm1 Then fz1 = fz1 - fm1
Loop Until fz1 = fm1 '约分
fz = fz / fz1
fm = fm / fm1
fs = fz & "/" & fm '此处的FZ为约分后的分子,FM为分母
End Function
2、函数调用举例:
dim a as single
a=0.125
msgbox fs(a)
展开全部
假设a=0.001
那么分数就是1/1000
依次类推(不是最简)
0.x小数变成分数:
Dim a as string
Dim i as single
i = 0.1345
Dim p() as string
Dim l as integer
a = cstr(i)
p = split(a,".")
l = len(p(1))
msgbox "分数为: " & trim(str(cdbl(p(1)))) & "/1" & string(l,"0")
如果有整数部分你自己写下 我没有时间了
那么分数就是1/1000
依次类推(不是最简)
0.x小数变成分数:
Dim a as string
Dim i as single
i = 0.1345
Dim p() as string
Dim l as integer
a = cstr(i)
p = split(a,".")
l = len(p(1))
msgbox "分数为: " & trim(str(cdbl(p(1)))) & "/1" & string(l,"0")
如果有整数部分你自己写下 我没有时间了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
'创建text1,Command1
Private Sub Command1_Click()
Dim a, i
Dim p() As String
Dim l As Integer
i = Val(Text1.Text)
a = CStr(i)
p = Split(a, ".")
l = Len(p(1))
MsgBox "整数为:" & Int(i) & vbCrLf & "分数为: " & Trim(Str(CDbl(p(1)))) & "/1" & String(l, "0")
End Sub
Private Sub Command1_Click()
Dim a, i
Dim p() As String
Dim l As Integer
i = Val(Text1.Text)
a = CStr(i)
p = Split(a, ".")
l = Len(p(1))
MsgBox "整数为:" & Int(i) & vbCrLf & "分数为: " & Trim(Str(CDbl(p(1)))) & "/1" & String(l, "0")
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询