Re: evaluating simple mathematical expressions
- From: "Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@xxxxxxxxxxxxxxxxx>
- Date: Mon, 14 Nov 2005 20:06:30 -0500
> 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
.
- Follow-Ups:
- Re: evaluating simple mathematical expressions
- From: Jim Edgar
- Re: evaluating simple mathematical expressions
- Prev by Date: Re: Menus always need resetting
- Next by Date: MSForms in VB6.0 Project - Cannot Quit
- Previous by thread: Re: evaluating simple mathematical expressions
- Next by thread: Re: evaluating simple mathematical expressions
- Index(es):
Relevant Pages
|