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...
pattern = "((?<num>\d+)/(?<den>\d+))|(?<dec>\d+\.?\d+)"
value = CInt(m.Groups("num").Value) / CInt(m.Groups("den").Value)
value = CDbl(m.Groups("dec").Value)
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