Rss & SiteMap
Foxtable(狐表) http://www.foxtable.com
如:
0.66667→2/3,
2.5→2 1/2
另:代码窗口中函数Floor的链接有误!
发现现在FoxTable中的数学函数反不如易表丰富,如易表中的Degrees,GCD,LCM,Quotient等还是有大作用嘀!
用代码最大的好处,是可以自己写,例如小数转分数的代码是:
Dim V As Double '要转换的小数
Dim N As Long '分子
Dim D As Long '分母
Dim MaxTerms As Long = 15 '最大循环次数
Dim MinDivisor As Double = 0.000001 '最小除数
Dim MaxError As Double = 0.00000001 '精度
Dim F As Double
Dim A As Long
Dim n1 As Long
Dim D1 As Long
Dim n2 As Long
Dim D2 As Long
F = V
n1 = 1
D1 = 0
n2 = 0
D2 = 1
For i As Long = 0 To MaxTerms
A = CLng(Math.Floor(F))
F = F - A
N = n1 * A + n2
D = D1 * A + D2
n2 = n1
D2 = D1
n1 = N
D1 = D
If F < MinDivisor Then Exit For
If Math.Abs(V - N / D) < MaxError Then Exit For
F = 1 / F
Next i
Return N & "/" & D
用代码最大的好处,是可以自己写,例如小数转分数的代码是:
Dim V As Double '要转换的小数
Dim N As Long '分子
Dim D As Long '分母
Dim MaxTerms As Long = 15 '最大循环次数
Dim MinDivisor As Double = 0.000001 '最小除数
Dim MaxError As Double = 0.00000001 '精度
Dim F As Double
Dim A As Long
Dim n1 As Long
Dim D1 As Long
Dim n2 As Long
Dim D2 As Long
F = V
n1 = 1
D1 = 0
n2 = 0
D2 = 1
For i As Long = 0 To MaxTerms
A = CLng(Math.Floor(F))
F = F - A
N = n1 * A + n2
D = D1 * A + D2
n2 = n1
D2 = D1
n1 = N
D1 = D
If F < MinDivisor Then Exit For
If Math.Abs(V - N / D) < MaxError Then Exit For
F = 1 / F
Next i
Return N & "/" & D
哈哈,这里除了您,没几人能写出上面的代码吧!
这个是表示成带分数的代码
思路是0.75 先写成 75/100
求最大公约数25 然后约分
Dim d As Double = -11.75 '要转化的小数
Dim d1str As String '最后结果的整数部分
Dim d2str As String '最后结果的分数部分
Dim d1 As Integer = Math.Truncate(d) 'd的整数部分,包括符号
If d1 <> 0 Then d1Str = d1 '如果整数部分不为0,则保存下来
If d.ToString.IndexOf(".") > -1 Then '如果有小数部分不为0
Dim d2 As Integer = d.ToString.Split(".")(1) '取得小数部分
Dim n As Integer = d2.ToString.Length '小数位数
Dim num As Integer = d2 '分子,未约分
Dim den As Integer = 10^n '分母,未约分
Dim x As Integer = num
Dim y As Integer = den
Dim temp As Integer
Do While x <> 0 'Do Loop 求最大公约数
temp = y Mod x
y = x
x = temp
Loop '循环结束,最大公约数保存在变量y中
d2str = num/y & "/" & den/y '约分
End If
Return d1str & " " & d2str
这个是表示成带分数的代码
思路是0.75 先写成 75/100
求最大公约数25 然后约分
Dim d As Double = -11.75 '要转化的小数
Dim d1str As String '最后结果的整数部分
Dim d2str As String '最后结果的分数部分
Dim d1 As Integer = Math.Truncate(d) 'd的整数部分,包括符号
If d1 <> 0 Then d1Str = d1 '如果整数部分不为0,则保存下来
If d.ToString.IndexOf(".") > -1 Then '如果有小数部分不为0
Dim d2 = d.ToString.Split(".")(1) '取得小数部分
Dim n As Integer = d2.ToString.Length '小数位数
Dim num As Integer = d2 '分子,未约分
Dim den As Integer = 10^n '分母,未约分
Dim x As Integer = num
Dim y As Integer = den
Dim temp As Integer
Do While x <> 0 'Do Loop 求最大公约数
temp = y Mod x
y = x
x = temp
Loop '循环结束,最大公约数保存在变量y中
d2str = num/y & "/" & den/y '约分
End If
Return d1str & " " & d2str
這個在適用的範圍,還挺精準轉化