如何显示分数不是小数格式

layola 2005-10-24 01:10:48
比如
a=1/2
我想输出是1/2,而不是0.5,怎么才可以?
...全文
321 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
northwolves 2005-10-26
  • 打赏
  • 举报
回复
多谢 northwolves(狼行天下)
你段代码无事,我想问下我段代码为什么遇到2/3会死机?
---------------------------------------------------

没有你说的情况

Function Friction(Fri As Single) As String
Const MAX = 0.01
Dim i As Integer
Dim str As String
i = 1
While Abs((i / Fri) - Round((i / Fri), 0)) > MAX
i = i + 1
Wend
str = i & "/" & Round(i / Fri)
Friction = str

End Function

Private Sub Command1_Click()
MsgBox Friction(0.6666666)
End Sub
layola 2005-10-25
  • 打赏
  • 举报
回复
多谢 northwolves(狼行天下)
你段代码无事,我想问下我段代码为什么遇到2/3会死机?
layola 2005-10-25
  • 打赏
  • 举报
回复
好象 遇到2/3就死机,
northwolves 2005-10-25
  • 打赏
  • 举报
回复
呀,我那段代码不能对付比如好像大于1的分数阿.那位大人帮忙该下.谢谢~
------------------
可以的,稍做修改:
Function Friction(Fri As Single, Optional digit As Integer = 2) As String
Dim i As Integer
Dim str As String
i = 1
While Abs((i / Fri) - Round((i / Fri), 0)) > 10 ^ (-digit)
i = i + 1
Wend
str = i & "/" & Round(i / Fri)
Friction = str
End Function


Private Sub Form_Load()
MsgBox Friction(3.14159, 2) & vbCrLf & Friction(3.14159, 4)'返回PI的疏率和密率
End Sub
layola 2005-10-25
  • 打赏
  • 举报
回复
呀,我那段代码不能对付比如好像大于1的分数阿.那位大人帮忙该下.谢谢~
northwolves 2005-10-24
  • 打赏
  • 举报
回复
layola(娉娉) 算得比较好,呵呵
layola 2005-10-24
  • 打赏
  • 举报
回复
seu31199113(Tony)寫的 ﹐高手
////////////////////////////////////////////
谢谢楼主给我灵感,我刚学VB一个星期!
=======
测试过了:str = Friction(0.142857142857) 结果是: 1/7
=======
Public Function Friction(Fri As Single) As String
Const MAX = 0.01
Dim i As Integer
Dim str As String
i = 1
While Abs((i / Fri) - Round((i / Fri), 0)) > MAX
i = i + 1
Wend
str = i & "/" & Round(i / Fri)
Friction = str

End Function
//////////////////////////
layola 2005-10-24
  • 打赏
  • 举报
回复
太长了罢 chewinggum

function Friction(Fri) {
max = 0.01;
i = 1;
var str;
dd = Math.round(i/Fri);
while (Math.abs((i/Fri)-dd)>max) {
i = i+1;
}
str = i+"/"+Math.round(i/Fri);
return (str);
}
cc=1/8
trace(Friction(0.333333333333333333333333333333333));
把 高手段代码改了格式,变成flash
脆皮大雪糕 2005-10-24
  • 打赏
  • 举报
回复
无限循环小数转换成分数要计算极限了,算法就比目前这个代码复杂了
Summer006 2005-10-24
  • 打赏
  • 举报
回复
这个得自己手动写个函数,来慢慢算了。
chewinggum(口香糖·把减肥列入下一个五年计划)那个有现成的,效果不错。
不过比如0.33333333333这种,他不会智能到显示1/3吧,那就有些厉害了。
faysky2 2005-10-24
  • 打赏
  • 举报
回复
想显示字符串,用两个双引号引起来才行
脆皮大雪糕 2005-10-24
  • 打赏
  • 举报
回复
郁闷,欧几里德算法获得最大公约数的算法G_cd原本没有下划线,但是会被论坛过滤。
上面的这段代码仅仅是一个示例,在数字范围上存在一定限制,希望搂主自行优化
winehero 2005-10-24
  • 打赏
  • 举报
回复
呵呵,chewinggum(口香糖·把减肥列入下一个五年计划)才完全正确。。。
脆皮大雪糕 2005-10-24
  • 打赏
  • 举报
回复
Option Explicit
Private Function Test(a As Single) As String
Dim strTmp As String
Dim strTmp1 As String
Dim intTmp As Integer
Dim intA As Integer
Dim intB As Integer
Dim intC As Integer
strTmp = Str(a)
strTmp1 = Left(strTmp, InStr(1, strTmp, ".") - 1)
strTmp = Right(strTmp, Len(strTmp) - InStr(1, strTmp, "."))
intA = Val(strTmp)
intB = 10 ^ Len(strTmp)
intC = G_cd(intA, intB)
If strTmp1 <> " " Then Test = strTmp1 & " + "
Test = Test & intA / intC & "/" & intB / intC
End Function

Private Function G_cd(ByVal a As Integer, ByVal b As Integer) As Integer
If a = 0 Then
G_cd = b
End If
If b = 0 Then
G_cd = a
End If
If a > b Then
Swap a, b
End If
Dim c As Integer
Do
c = a Mod b
a = b
b = c
DoEvents
Loop While c > 0
G_cd = a
End Function

Private Sub Swap(a As Integer, b As Integer)
Dim c As Integer
c = a
a = b
b = c
End Sub

测试用例:
Debug.Print Test(0.5)
Debug.Print Test(0.125)
Debug.Print Test(6.008)

测试输出:
1/2
1/8
6 + 1/125
zou19820704 2005-10-24
  • 打赏
  • 举报
回复
楼上完全正确!呵呵
northwolves 2005-10-24
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim a As String
a = "1/2"
Print a
End Sub

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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