Skip to Content
avatar image
Former Member

matrix read out numbers only - VB.Net code sample

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

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

1 Answer

  • avatar image
    Former Member
    Jan 27, 2004 at 04:30 PM

    it is too complecated for me.

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

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      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