Re: evaluating simple mathematical expressions
- From: Suz <Suz@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 17 Nov 2005 12:05:03 -0800
Thanks so much! What's entertaining is that the next operation that I had to
do involved doing a lookup from cells in Excel where the column headers were
fractions, as opposed to my previous case where the input values were
fractions. I found that if you take a cell in Excel and do a
myCell.Find("1/4"), it fails. It is perfectly happy to work on
myCell.Find(".25"), so I guess the internal storage for the fractionally
formatted cell is a double? (Obviously, I am NOT an Excel user). Your
function will come in REALLY handy here! Of course, it is interesting to
wonder how well Excel will "match" a floating point number - as I mentioned,
I'm very careful not to directly compare two floating points, cause they
almost never match after computation...
Suz
"Rick Rothstein [MVP - Visual Basic]" wrote:
> > You'll need to add some division-by-zero error checking and also
> check to
> > see if the slash has a number on either side of it. Here's a
> couple of
> > results:
> >
> > 1 7/8 --> 1.875
> > 1 /8 --> .125
> > /8 ---> 0
> > 7/ --> division by zero
> > 7/0 --> division by zero
>
> Yeah, it was **old** code. Thanks for spotting those. I've updated
> the function to VB6 (only) standards and (hopefully) addressed all
> of the error possibilities now. Below is the modified function.
>
> Rick
>
> Function FracToDec(ByVal Fraction As String) As Double
> Dim Blank As Integer
> Dim Slash As Integer
> Dim CharPosition As Integer
> Dim WholeNumber As Integer
> Dim Numerator As Integer
> Dim Denominator As Integer
> 'Remove leading and trailing blanks
> Fraction = Trim$(Fraction)
> 'Collapse all multiple blanks to a single blank
> Do While InStr(Fraction, " ")
> Fraction = Replace$(Fraction, " ", " ")
> Loop
> 'Remove any space character after the slash
> Fraction = Replace$(Fraction, "/ ", "/")
> 'Remove any space character in front of the slash
> Fraction = Replace$(Fraction, " /", "/")
> 'Locate the blank and/or slash
> Blank = InStr(Fraction, " ")
> Slash = InStr(Fraction, "/")
> 'The Fraction argument can't have characters other than
> 'blanks, slashes, digits and it can only have one blank
> 'and/or one slash. You can modify this error section
> 'as appropriate to your needs.
> If Fraction Like "*[! /0-9]*" Or Fraction Like "/*" Or _
> Fraction Like "*/" Or Fraction Like "*/*/*" Or _
> Fraction Like "*/0" Or (Blank > 0 And Slash = 0) Then
> MsgBox "Error -- Improperly formed expression"
> 'The Fraction argument is now in one of these formats
> 'where # stands for one or more digits: #, # #/# or #/#
> Else
> 'There is no slash (Format: #)
> If Slash = 0 Then
> FracToDec = Val(Fraction)
> 'There is a slash, but no blank (Format: #/#)
> ElseIf Blank = 0 Then
> FracToDec = Val(Left$(Fraction, Slash - 1)) / _
> Val(Mid$(Fraction, Slash + 1))
> 'There are both a slash and a blank (Format: # #/#)
> Else
> FracToDec = Val(Left$(Fraction, Blank - 1)) + _
> Val(Mid$(Fraction, Blank + 1, _
> Slash - Blank - 1)) / _
> Val(Mid$(Fraction, Slash + 1))
> End If
> End If
> End Function
>
>
>
.
- References:
- Re: evaluating simple mathematical expressions
- From: Rick Rothstein [MVP - Visual Basic]
- Re: evaluating simple mathematical expressions
- From: Jim Edgar
- Re: evaluating simple mathematical expressions
- Prev by Date: Re: VB6 Generating Lots of Network Traffic
- Next by Date: check if port is blocked
- Previous by thread: Re: evaluating simple mathematical expressions
- Next by thread: Re: Menus always need resetting
- Index(es):
Relevant Pages
|