Converting a decimal to a fraction
Author Message Converting a decimal to a fraction

Has anyone written code or seen an example to convert a decimal into a
fraction?

Thanks,

Craig Buchanan, President
Cogniza, Inc.
Seagate Software Partner
http://www.*-*-*.com/

+1.612.824.5858

Sun, 07 Oct 2001 03:00:00 GMT  Converting a decimal to a fraction
Depends. Do you have a fixed list of decimals to convert (e.g. 0.25, 0.125,
0.0125, 0.375, etc) or do you need something that can work with any decimal
input?

In the first case you could use some sort of lookup table, and in the second
case, you could try:

move decimal position to end of fractional part
store numerator and denominator (denominator = 10 ^ number of positions
decimal moved)
divide both by highest factor common to both.

e.g. .23345 -> 23345/100000 -> 4669/20000

Colin

Quote:
> Has anyone written code or seen an example to convert a decimal into a
> fraction?

> Thanks,

> Craig Buchanan, President
> Cogniza, Inc.
> Seagate Software Partner
> http://www.cogniza.com

> +1.612.824.5858

Sun, 07 Oct 2001 03:00:00 GMT  Converting a decimal to a fraction
I wrote a routine that is designed to convert stock market decimal
values to fractions. In this case, it's necessary to know the smallest
fraction value, and that all values be multiples of that value. For
example, if you're unlucky enough to own stock in Boston Chicken, you
holding are reported in multiples of 1/256th of a dollar. In this
case, you would pass 256 as the value for the iDenom parameter. Making
this a larger-than-necessary value won't affect the outcome, it will
just make the routine run a new extra cycles. snValue is the decimal
value you are converting. The result will be a string in the form
x xxx/xxx
There will be a leading zero if the value is less than one. Only
positive values are handled (due to the design of the stock data
output) but you could easily add code for that.

Here's the code:

-----------------Start of Code-------------------

Public Function Dec2Frac(ByVal snValue As Single, _
ByVal iDenom As Integer) As String

'  Used to convert stock prices from decimal to fractions.
'  The iDenom is the highest denominator (16, 32, 256, etc. for
'  stock prices).

Dim iWork As Integer
Dim iInt As Integer
Dim iFrac As Single

iInt = Int(snValue)
iFrac = snValue - iInt

iWork = iFrac * iDenom

Do While iWork Mod 2 = 0
iWork = iWork / 2
iDenom = iDenom / 2
Loop

Dec2Frac = Format(iInt, "0 ") & Format(iWork, "0/") & _
Format(iDenom)

End Function

-----------------End of Code---------------------

Quote:
>Has anyone written code or seen an example to convert a decimal into a
>fraction?

>Thanks,

>Craig Buchanan, President
>Cogniza, Inc.
>Seagate Software Partner
>http://www.cogniza.com

>+1.612.824.5858

Sun, 07 Oct 2001 03:00:00 GMT  Converting a decimal to a fraction
Here is a fairly versatile approach:

Private Function CFraction(ByVal Num) As String
Dim Recip As Single, Denom As Long
Dim Out As String
Const Tick = 2048 '1/Tick = Smallest allowable increment

Num = CSng(Num)
If Num >= 1 Then
Out = CStr(Int(Num)) & " "
Num = Num - Int(Num)
End If
If Num = 0 Then GoTo SkipFraction
Recip = Fix(Num * Tick)
Denom = Tick
Do While (Recip And 1) = 0
Recip = Recip \ 2
Denom = Denom \ 2
Loop
Out = Out & CStr(Recip) & "/" & CStr(Denom)
SkipFraction:
CFraction = Out
End Function

LFS

Quote:

> Has anyone written code or seen an example to convert a decimal into a
> fraction?

> Thanks,

> Craig Buchanan, President
> Cogniza, Inc.
> Seagate Software Partner
> http://www.cogniza.com

> +1.612.824.5858

Sun, 07 Oct 2001 03:00:00 GMT  Converting a decimal to a fraction

Fri, 19 Jun 1992 00:00:00 GMT  Converting a decimal to a fraction
A somewhat less wordy approach:

Private Function CFraction(ByVal Num) As String
Dim Recip As Single, Denom As Long
Dim Out As String
Const Tick = 2048 '1/Tick = Smallest allowable increment

Num = CSng(Num)
If Num < 0 Then
Out = "-"
Num = Abs(Num)
End If
If Num >= 1 Then
Out = Out & CStr(Int(Num)) & " "
Num = Num - Int(Num)
End If
If Num > 0 Then
Recip = Int(Num * Tick)
Denom = Tick
Do While (Recip And 1!) = 0
Recip = Recip \ 2
Denom = Denom \ 2
Loop
Out = Out & CStr(Recip) & "/" & CStr(Denom)
End If
CFraction = Out
End Function

Who said writing less code, can't be fun???

LFS

Mon, 15 Oct 2001 03:00:00 GMT

 Page 1 of 1 [ 8 post ]

Relevant Pages