RE: Converting a fraction to decimal

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Thanks Dave,
I wrote the following function and it hung up on the Line: aryAllParts =
Split(strNumber, "/") with the message "Cannot assign to array."

Public Function FractionToDecimal(txtFraction As String) As Single
'Set up a variable the Split function can use. So we can separate the
'number and both sides of the fraction into an array:

Dim strNumber As String
Dim aryAllParts(2) As String
strNumber = Replace(Trim(txtFraction), " ", "/")
'Create the array:

aryAllParts = Split(strNumber, "/")

'Element 0 is the whole number
'Element 1 is left side of the fraction
'Element 2 is the right side of the fraction

'Do the math and put it to the function value
FractionToDecimal = CSng(CStr(aryAllParts(0)) & Format(aryAllParts(1) /
aryAllParts(2), "#.0000"))

End Function
I would appreciate your comments on how to fix this even though Fred G has
given me another function.
Thaks,
George R.
"Klatuu" wrote:

Let's say we have a text box named txtFraction to put the enter number and
fraction in and we want it to show in decimal format in a control named
txtDecimal

'Set up a variable the Split function can use. So we can separate the
number and bothi sides of the fraction into an array:

strNumber = Replace(Trim(Me.txtFraction), " ", "/")

'Create the array:

aryAllParts = Split(strNumber, "/")

'Element 0 is the whole number
'Element 1 is left side of the fraction
'Element 2 is the right side of the fraction

'Do the math and put it in the txtDecimal Control

Me.txtDecimal = cstr(aryAllParts(0)) & format(aryAllParts(1) /
aryAllParts(2), "#.0000")

You can change the formatting to suit yourself, but the leading # in format
string is to surpress the 0 that is alway on the front of a number less than
1.

If I were doing this, I would write a function that would return the
formatted string and call it from txtDecimal's control source

--
Dave Hargis, Microsoft Access MVP


"George R" wrote:

I would like to use an unbound control on an unbound form to specify
parameters for a query. I would like the user to enter inches and then
fractional inches and then have another text box show the total of those two
entries in decimal form. How can I convert an entry such as 1/8 inches to
0.125 inches?
Or, if possible, have the user enter inches and fractions in the same box
and convert to decimal, eg: 4 1/2 to 4.500.
Thank you for your consideration.
.



Relevant Pages

  • Re: Why does Format.String do rounding?
    ... rounding because there is no decimal point in your format string. ... the number because he doesn't want the fraction". ... I would like regPriceString to wind up with "000002495000+" I don't want ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How do I format Concatenate function?
    ... into one string. ... inches part of it comes out in decimals, not as a fraction, ie. ...
    (microsoft.public.excel.misc)
  • Re: How do I format Concatenate function?
    ... into one string. ... inches part of it comes out in decimals, not as a fraction, ie. ...
    (microsoft.public.excel.misc)
  • Re: Math.PI.toString(16) -> 3.243f6a8885a3 and back
    ... Try almost any fraction to a large odd base on Chrome; ... so the string has more resolution than an IEEE Double can give; ... Your parseFraction seems to loop over all of the fraction digits, ... parseInt on the fractional part should be better, ...
    (comp.lang.javascript)
  • spacing issue with drawing a string into a JScrollPane
    ... The string is a representation of a whole number & fraction number group. ... int wholeNumberStringWidth; ... wholeNumberFractionGap, y - 10); ...
    (comp.lang.java.gui)