如何编写一个小数转换分数的函数?
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
上一篇: 想让你看看高兴高兴