ASP
--
Questions
--
Followers
Top Experts
Convert decimals to fractions
Hello experts ~
I have found this bit of code which seems to work well in converting decimals to fractions. Here is the code I am using:
************************** *********
Function DecimalToFraction(dec, mixed)
'Input parameter(s):
' dec [single] The decimal value to be converted to a fraction.
' mixed [boolean]
' True = Return a mixed number if dec > 1
' False = Return an improper fraction if dec > 1
' ex.: 3.25 equals "13/4" as an improper fraction and "3 1/4" as a mixed number.
Dim ds, fn, fd, pd, tmp, acc, oi
ds = 1
If (dec < 0) Then ds = -1
dec = abs(dec)
If mixed Then
oi = cstr(int(dec))
dec = (dec - oi)
End If
If dec = int(dec) Then
fn = (dec * ds)
fd = 1
Else
z = dec : pd = 0 : fd = 1 : acc = .5
Do
acc = (acc * 0.1)
Do
z = 1 / (z - int(z))
tmp = fd
fd = fd * int(z) + pd
pd = tmp
fn = int(dec * fd + 0.5)
Loop Until (abs((dec - (fn / fd))) < acc) or (z = int(z))
fn = (ds * fn)
Loop While Not (fn/fd) = csng(dec)
End If
If mixed Then
DecimalToFraction = oi & " " & cstr(fn) & "/" & cstr(fd)
Else
DecimalToFraction = cstr(fn) & "/" & cstr(fd)
End If
End Function
************************** *********
The problem is if the value I feed the function is not exactly a decimal such as 4 or even 4.0 then it displays it as : 4 0/1
Is there an easy fix to this code or is there an even better / easier way to convert decimals to fractions?
Thanks so much in advance,
webdude ~
I have found this bit of code which seems to work well in converting decimals to fractions. Here is the code I am using:
**************************
Function DecimalToFraction(dec, mixed)
'Input parameter(s):
' dec [single] The decimal value to be converted to a fraction.
' mixed [boolean]
' True = Return a mixed number if dec > 1
' False = Return an improper fraction if dec > 1
' ex.: 3.25 equals "13/4" as an improper fraction and "3 1/4" as a mixed number.
Dim ds, fn, fd, pd, tmp, acc, oi
ds = 1
If (dec < 0) Then ds = -1
dec = abs(dec)
If mixed Then
oi = cstr(int(dec))
dec = (dec - oi)
End If
If dec = int(dec) Then
fn = (dec * ds)
fd = 1
Else
z = dec : pd = 0 : fd = 1 : acc = .5
Do
acc = (acc * 0.1)
Do
z = 1 / (z - int(z))
tmp = fd
fd = fd * int(z) + pd
pd = tmp
fn = int(dec * fd + 0.5)
Loop Until (abs((dec - (fn / fd))) < acc) or (z = int(z))
fn = (ds * fn)
Loop While Not (fn/fd) = csng(dec)
End If
If mixed Then
DecimalToFraction = oi & " " & cstr(fn) & "/" & cstr(fd)
Else
DecimalToFraction = cstr(fn) & "/" & cstr(fd)
End If
End Function
**************************
The problem is if the value I feed the function is not exactly a decimal such as 4 or even 4.0 then it displays it as : 4 0/1
Is there an easy fix to this code or is there an even better / easier way to convert decimals to fractions?
Thanks so much in advance,
webdude ~
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
ASKER CERTIFIED SOLUTION
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
hehe, dunno if I was just lazy or not thinking. (maybe both) ;)
ok it works and thats good enough for me..
Thanks!
ok it works and thats good enough for me..
Thanks!






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
ASP
--
Questions
--
Followers
Top Experts
Active Server Pages (ASP) is Microsoft’s first server-side engine for dynamic web pages. ASP’s support of the Component Object Model (COM) enables it to access and use compiled libraries such as DLLs. It has been superseded by ASP.NET, but will be supported by Internet Information Services (IIS) through at least 2022.