Converting decimals to fractions

# Thread: Converting decimals to fractions

1. Junior Member
Join Date
Dec 1969
Posts
4

## Converting decimals to fractions

I used this VB6 code (which was suggested to me) to convert Decimals to fractions which I call in the body by<BR><BR>DecToFrac(DataBinder.Eval (Container.DataItem, "ShredSizeWidth"), a, b) so it an convert,<BR><BR>for example 0.125 to 1/8"<BR><BR>but I get the following error:<BR><BR>Compiler Error Message: BC30451: Name &#039;a&#039; is not declared.<BR><BR>The subs are:<BR><BR>Sub DecToFrac(DecimalNum As Double, Numerator As Long, Denom As Long)<BR> &#039; The BigNumber constant can be adjusted to handle larger fractional parts<BR> Const BigNumber = 50000<BR> Const SmallNumber = 1E-16<BR><BR> Dim Inverse As Double, FractionalPart As Double<BR> Dim WholePart As Long, SwapTemp As Long<BR> <BR> Inverse = 1 / DecimalNum<BR> WholePart = Int(Inverse)<BR> FractionalPart = Frac(Inverse)<BR> <BR> If 1 / (FractionalPart + SmallNumber) &#060; BigNumber Then<BR> &#039; Notice that DecToFrac is called recursively. <BR> Call DecToFrac(FractionalPart, Numerator, Denom)<BR> Numerator = Denom * WholePart + Numerator<BR> <BR> SwapTemp = Numerator<BR> Numerator = Denom<BR> Denom = SwapTemp<BR><BR> Else &#039; If 1 / (FractionalPart + SmallNumber) &#062; BigNumber<BR> &#039; Recursion stops when the final value of FractionalPart is 0 or<BR> &#039; close enough. SmallNumber is added to prevent division by 0.<BR><BR> Numerator = 1<BR> Denom = Int(Inverse)<BR> End If<BR>End Sub<BR><BR>&#039; This function is used by DecToFrac and DecToProperFact<BR><BR>Function Frac(x As Double) As Double<BR> Frac = Math.Abs(Math.Abs(x) - Int(Math.Abs(x))) <BR>End Function<BR><BR>&#039; This additional procedure handles "improper" fractions and returns<BR>&#039; them in mixed form (a b/c) when the numerator is larger than the denominator<BR><BR>Sub DecToProperFrac(x As Double, a As Long, b As Long, c As Long)<BR> If x &#062; 1 Then a = Int(x)<BR> If Frac(x) &#060;&#062; 0 Then <BR> Call DecToFrac(Frac(x), b, c)<BR> End If<BR>End Sub<BR><BR><BR>Please Help.

2. Senior Member
Join Date
Dec 1969
Posts
655

## I'm full of "one thoughts" this morning

One thought - <BR>You pass &#039;a&#039; into your DecToProperFrac by Value and then you try to set the value of it which you shouldn&#039;t be able to do. Try stricking ByRef in front of it like Sub DecToProperFrac(ByRef a As Long) If that is what you acutally want to do.<BR>

3. Junior Member
Join Date
Dec 1969
Posts
4

## RE: I'm full of

I placed it in the sub<BR><BR>Sub DecToProperFrac(x As Double, ByRef a As Long, ByRef b As Long, c As Long)<BR><BR>and it still didn&#039;t work, but thanks!!!

4. Senior Member
Join Date
Dec 1969
Posts
655

## On further analysis

Didn&#039;t notice your subroutine call in your original message. The error is probally on that line. Where do you declare &#039;a&#039; above there?

5. Junior Member
Join Date
Dec 1969
Posts
4

## RE: On further analysis

DecToFrac(DataBinder.Eval (Container.DataItem, "ShredSizeWidth"), a, b) this is the specific call (correct?)<BR><BR>Are you tellin me to declare it like this?<BR><BR>Dim a As Long<BR>Dim B As Long<BR><BR>I&#039;m new to ASP.Net, so I apologize for the elementry questions, and thank you for your help!!!<BR><BR>And I&#039;m calling it in this IIF statement:<BR><BR>&#060;%# IIf (DataBinder.Eval(Container.DataItem, "ShredSizeWidth") Is DBNull.Value, "&#060;tr&#062;&#060;td width=""285""&#062;&#060;font size=""2"" face=""Verdana, Arial, Helvetica, sans-serif""&#062; Shred Size&#060;/strong&#062;&#060;/font&#062;&#060;/td&#062;&#060;td width=""285""&#062;&#060;font size=""2"" color=""#FF0000""face=""Verdana, Arial, Helvetica, sans-serif""&#062; EMPTY&#060;/font&#062;&#060;/td&#062;&#060;/tr&#062;", "&#060;tr&#062;&#060;td width=""285""&#062;&#060;font size=""2"" face=""Verdana, Arial, Helvetica, sans-serif""&#062;&#060;strong&#062; Shred Size&#060;/strong&#062;&#060;/font&#062;&#060;/td&#062;&#060;td width=""285""&#062;&#060;font size=""2"" face=""Verdana, Arial, Helvetica, sans-serif""&#062; " & DecToFrac(DataBinder.Eval(Container.DataItem, "ShredSizeWidth"), a, b) & "&#060;/font&#062;&#060;/td&#062;&#060;/tr&#062;") %&#062;

6. Senior Member
Join Date
Dec 1969
Posts
655

## RE: On further analysis

Ok it appears you are calling the function with a bound item, and then two black variables but you dont declare the variables any where before you pass them to the function. I&#039;m pretty sure that is what the problem is. It doesn&#039;t appear that you need to pass them either.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•