cancel
Showing results for 
Search instead for 
Did you mean: 

matrix read out numbers only - VB.Net code sample

Former Member
0 Kudos

Hi all,

instead of asking yet another question, I would like to publish a helpful bit of code as solution to a problem concerning the matrix item.

Problem:

When reading out specific cells containing currency values, SBO returns a string including the currency. (ie "1.000,95 EUR") This value obviously cannot be casted into a double value.

Here is a code snippet which takes care of the issue. Glad for any comments on this.

HTH Lutz Morrien

Public Class NumbersInStr

  Public Shared Function AsString(ByVal StringToFilter As String) As String

    ' This function takes a string as input and filters out all chars

    ' which do not appear in the string GoodChars

    ' Example: Input "220,00 EUR"

    ' Output "220,00"

    Dim intCharCount As Integer

    Dim GoodChars As String

    Dim strResult As String

    'if using english format, replace "," by "."

    GoodChars = "12345677890,"

    'go through string

    For intCharCount = 0 To StringToFilter.Length - 1

      'if char at current location is a "good char",

      ' then move it to result string

      If GoodChars.IndexOf(StringToFilter.Chars(intCharCount)) >= 0 Then

        strResult = strResult & StringToFilter.Chars(intCharCount)

      End If

    Next

    If strResult <> "" Then

      Return strResult

    Else

      Return "0"

    End If

  End Function

  Public Shared Function AsDouble(ByVal MixedString As String) As Double

    Return CDbl(AsString(MixedString))

  End Function

  Public Shared Function AsSingle(ByVal MixedString As String) As Single

    Return CSng(AsString(MixedString))

  End Function

End Class

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

it is too complecated for me.

why don't you use System.Text.RegularExpressions.Regex object ?

Former Member
0 Kudos

Jarek,

i am always interested in a better solution. I cannot not find a suitable method in System.Text.RegularExpressions.Regex. Could you give a code example that simplifies the code I wrote and does the same?

Actually, I would prefer to access the datasources instead of the matrix and do away with the code altogether.

TIA Lutz Morrien

Former Member
0 Kudos

Hi Lutz, here you are (C# example) :

double val = 0.0;

string rexp = "(?[0-9-])(?<val>[0-9-.,])(?[^0-9]*)";

string inp = "eur -1.000,95 EUR";

System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(rexp);

System.Text.RegularExpressions.Match m = r.Match(inp);

if(m.Success)

  val = Convert.ToDouble(m.Groups["val"].Value.Replace("." , ""));

MessageBox.Show(val.ToString());

have a good time, greetings from Poland

Jarek

jarek.kocik@yuma.pl