Question RegularExpression to convert from Fraction to decimal

tim8w

Active member
Joined
Oct 26, 2010
Messages
38
Programming Experience
5-10
I need a RegularExpression that can convert a string from a fraction or decimal like "1/2" or "0.5" to a decimal "0.5". It needs to handle both cases. Input can be either fractional or decimal, output must be decimal...
 
If you're happy with "." as decimal separator then for example this:
VB.NET:
pattern = "((?<num>\d+)/(?<den>\d+))|(?<dec>\d+\.?\d+)"
Then if m.Groups("num").Success get
VB.NET:
value = CInt(m.Groups("num").Value) / CInt(m.Groups("den").Value)
Else get
VB.NET:
value = CDbl(m.Groups("dec").Value)
According to Wiki percentage is also a fraction, so you could include that too.
 
JohnH,
Got it to run a little, but it only worked for patterns like "1/2", "3/4" but not for "1 1/2", "1 3/4" or "3.14"...

Here's what I coded:

Dim dOrigValue As Double
Dim sPattern As String = "((?<num>\d+)/(?<den>\d+))|(?<dec>\d+\.?\d+)"
Dim regExp As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(sPattern, RegexOptions.Compiled)
Dim m As Match = regExp.Match(dgvc.Value)

If m.Success Then
dOrigValue = CInt(m.Groups("num").Value) / CInt(m.Groups("den").Value)
Else
dOrigValue = CDbl(m.Groups("dec").Value)
End If
 
Surely the 'dec' pattern fits the "3.14" string :) I think you should be able to expand the fraction side of the expression to include mixed number fractions given what you now know.
 
JohnH,
The 'dec' pattern may fit the pattern, but how do I know which part of the pattern it matched? If I just ask m.Success I don't know which part was successful...
 
which part of the pattern it matched?
I talked about that in the first reply.
 
I ended up using four different RegEx to catch all the cases:

VB.NET:
sPattern = "((?<whole>\d+) (?<num>\d+)/(?<den>\d+))"    ' 1 1/2 case
regExp = New System.Text.RegularExpressions.Regex(sPattern, RegexOptions.Compiled)
m = regExp.Match(dgvc.Value)

If m.Success Then
    dDecValue = CInt(m.Groups("whole").Value) + CInt(m.Groups("num").Value) / CInt(m.Groups("den").Value)
Else
    sPattern = "((?<num>\d+)/(?<den>\d+))"              ' 3/4 case
    regExp = New System.Text.RegularExpressions.Regex(sPattern, RegexOptions.Compiled)
    m = regExp.Match(dgvc.Value)
    If m.Success Then
        dDecValue = CInt(m.Groups("num").Value) / CInt(m.Groups("den").Value)
    Else
        sPattern = "(?<dec>\d+\.?\d+)"                  ' 0.5 case
        regExp = New System.Text.RegularExpressions.Regex(sPattern, RegexOptions.Compiled)
        m = regExp.Match(dgvc.Value)
        If m.Success Then
            dDecValue = CDbl(m.Groups("dec").Value)
        Else
            sPattern = "(?<whole>\d+)"                  ' 2 case
            regExp = New System.Text.RegularExpressions.Regex(sPattern, RegexOptions.Compiled)
            m = regExp.Match(dgvc.Value)
            If m.Success Then
                dDecValue = CDbl(m.Groups("whole").Value)
            Else
                MsgBox("Error in value")
            End If
        End If
    End If
End If
 
Back
Top