Re: evaluating simple mathematical expressions




"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@xxxxxxxxxxxxxxxxx>
wrote in message news:ehj3QDY6FHA.3760@xxxxxxxxxxxxxxxxxxxxxxx
> > I've got a bunch of truly fractional expressions, i.e. "5/16"
> that are
> > Strings (be it from a database lookup or a user interface object
> such as a
> > listbox).
> >
> > What is the simplest way of converting a fractional string like
> "5/16" to a
> > Double (in this case, I want 0.3125)?
> >
> > Also, what's the simplest way to test if a fractional entry is a
> valid
> > numeric expression. I want to accept either true fractions,
> integers, or
> > floating point numbers. Unfortunately, IsNumeric() does not see
> a String
> > like "5/16" as numeric. A type conversion function like CDbl
> gives a type
> > mismatch and Val simply grabs the first digit. Some entries
> from the VB6
> > Immediate window follow:
>
> Perhaps this function which I posted a couple of years ago might
> do what you. Note that a blank was assumed to separate the whole
> number from the fraction for this question... you can change that
> if you wish (assuming you use a dash instead) or even modify the
> function to add an optional argument defaulting to one of them and
> allowing the other to be specified optionally.
>
> 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
> CharPosition = InStr(Fraction, " ")
> Do While CharPosition
> Fraction = Left$(Fraction, CharPosition) & _
> Mid$(Fraction, CharPosition + 2)
> CharPosition = InStr(Fraction, " ")
> Loop
> 'Remove any space character after the slash
> CharPosition = InStr(Fraction, "/ ")
> If CharPosition Then
> Fraction = Left$(Fraction, CharPosition) & _
> Mid$(Fraction, CharPosition + 2)
> End If
> 'Remove any space character in front of the slash
> CharPosition = InStr(Fraction, " /")
> If CharPosition Then
> Fraction = Left$(Fraction, CharPosition - 1) & _
> Mid$(Fraction, CharPosition + 1)
> End If
> '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 _
> InStr(Blank + 1, Fraction, " ") Or _
> InStr(Slash + 1, Fraction, "/") 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
>
>

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

Jim Edgar


.



Relevant Pages

  • Re: evaluating simple mathematical expressions
    ... do involved doing a lookup from cells in Excel where the column headers were ... >> see if the slash has a number on either side of it. ... > Function FracToDec(ByVal Fraction As String) As Double ... > Dim Blank As Integer ...
    (microsoft.public.vb.general.discussion)
  • Re: Find AND Fix Fractions
    ... Sub FindFixFractions() ... Dim rng As Word.Range ... Dim pStr As String ... the end of this note] but the fraction needs to be selected in order ...
    (microsoft.public.word.vba.beginners)
  • Re: accept fractions from textbox
    ... Function FracToDec(ByVal Fraction As String) As Double ... Dim myValue As Double ... I am having trouble getting this to work in Excel 2007; ...
    (microsoft.public.excel.programming)
  • Re: evaluating simple mathematical expressions
    ... > What is the simplest way of converting a fractional string like ... Function FracToDec(ByVal Fraction As String) As Double ... Dim Blank As Integer ... Dim Slash As Integer ...
    (microsoft.public.vb.general.discussion)
  • Re: How to add Fractions along with their formatting as AutoText in a template
    ... > Yes I have some fraction macros and some of the images. ... > Dim oNum As String ... > Dim bInvalid As Boolean ...
    (microsoft.public.word.customization.menustoolbars)