当前位置:主页   - 电脑 - 网站开发 - ASP
我自己编的,可能大家有时会用上,函数小数转分数,限于整除分数,如1/8,5/4等
来源:网上收集   作者:未知   更新时间:2009-01-31
收藏此页】    【字号    】    【打印】    【关闭
'函数实现小数转分数
Public Function XtoF(str As Currency, Optional fenm As Integer = 32) As String '只限于整除分数
   Dim Cfm As Currency
   Dim cfmmod As Integer
   On Error GoTo Erroreof
   
   Cfm = 1 / fenm
   XtoF = ""
   If str = 0 Then XtoF = "": Exit Function
   
   Dim point As Integer
   Dim dInt As String
   Dim dPoint As Currency
   Dim fint, fint1, fint2 As Integer
   If str <> 0 Then
        If str > 1 Then
            point = InStr(1, str, ".", 1)
            If point = 0 Then
                XtoF = str:
                Exit Function
            Else
                dInt = Mid(str, 1, point - 1)
                dPoint = CCur("0." & Mid(str, point + 1))
                fint = InStr(1, XtoF(dPoint), "/", 1)
                fint1 = CInt(Mid(XtoF(dPoint), 1, fint - 1))
                fint2 = CInt(Mid(XtoF(dPoint), fint + 1))
                
                XtoF = CStr(dInt * fint2 + fint1) & "/" & CStr(fint2)
           
            End If
        Else
           If fenm Mod CInt(str / Cfm) = 0 Then
            XtoF = "1/" + CStr(fenm / CInt(str / Cfm))
           Else
              cfmmod = Maxgys(fenm, CInt(str / Cfm))
            XtoF = CStr(CInt(str / Cfm / cfmmod)) + "/" + CStr(CInt(fenm / cfmmod))
           End If
        End If
   Else
       XtoF = "0"
   End If
   Exit Function
Erroreof:
   XtoF = ""
End Function
Function Maxgys(num1 As Integer, num2 As Integer) As Integer
    Dim minnum, i As Integer
    minnum = num1
    If num1 > num2 Then minnum = num2
    For i = 1 To minnum
     If ((num1 Mod i) = 0) And ((num2 Mod i) = 0) Then Maxgys = i

  
    Next i
End Function

其它资源
来源声明

版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明