convert fractions to decimals

B

bob gee

i have the need to convert fractioal numbers to decimals.
An example is 27-13/16" (or 27.13/16"). This number is 27
and 13/16"). I understand that I have to have three
fields, one for the entry, one for the formula, and a
third for the result. I just don't know how to express the
formula without using more fields. I don't want to
seperate the whole number from the fraction in two
different fields. I would prefer to use the hyphen as the
seperator, as opposed to the decimal, but I'll take what I
can get.
 
M

Marshall Barton

bob said:
i have the need to convert fractioal numbers to decimals.
An example is 27-13/16" (or 27.13/16"). This number is 27
and 13/16"). I understand that I have to have three
fields, one for the entry, one for the formula, and a
third for the result. I just don't know how to express the
formula without using more fields. I don't want to
seperate the whole number from the fraction in two
different fields. I would prefer to use the hyphen as the
seperator, as opposed to the decimal, but I'll take what I
can get.


You don't need any additional fields to calculate the value
of that kind of string. You can just use an expression to
convert it whenever needed. If you use a + as the
separator, then this is all it takes:

Eval("27 + 13/16")

If you want to use - as a separator, then you can do this:

Eval(Replace("27 - 13/16", "-", "+"))

Note that spaces are optional with this approach.
 
B

bob gee

I haven't tried your suggestion, because I couldn't
figure out where to plug the info. It looked like i'd
have to type all that for each field (up to 10 on a
form). I would still need to show both fields, the
fractional and decimal, and store them back onto the
recording table. Some people have to read the output and
some use decimal and others use fractional. The other
response seemd to work well but it had a few drawbacks.
 
M

Marshall Barton

bob said:
I haven't tried your suggestion, because I couldn't
figure out where to plug the info. It looked like i'd
have to type all that for each field (up to 10 on a
form). I would still need to show both fields, the
fractional and decimal, and store them back onto the
recording table. Some people have to read the output and
some use decimal and others use fractional. The other
response seemd to work well but it had a few drawbacks.


Since that is just a function call, you can use it anywhere
you can use an expression. If you don't want to write the
whole thing everywhere, then wrap it in a public user
defined function:

Public Function CvFraction(strFrac As String) As Double
CvFraction = Eval(Replace(strFrac , "-", "+"))
End Function

Then whenever you need to convert a fraction string to a
number just call the function.

If you want to allow users to enter either a fraction or an
actual value then you can use the fraction text box's
AfterUpdate event to set the value of the other text box:

Me.txtDecimal = CvFraction(Me.txtFraction)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top