Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
Could someone please assist? I have a string which is like monetary value. I just want to grab the actual amount excluding the currency symbols.
E.g. I have two amounts as NZD12453,25 and AUD2356,
What I want to grab is 12453.25 and 2356
Insetad of the comma a period will be placed if there are values after the comma, if no values after the comma then just remove the comma and the letters infront like AUD and the NZD and grab only the numbers.
Pease assist. my code below is working but it's giving the amount removing the comma.
VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       Dim B32Stinrg As String = "JPY12561694,60"
       Dim B32Stinrg2 As String = "JPY12532694,"
       Dim test2 As String = "JPY32561694,:77CDX"

       Dim TestDisplay1 As String = GetNumbersfromStringUsingSB(B32Stinrg.Replace(":32B:", ""))
       Dim TestDisplay2 As String = GetNumbersfromStringUsingSB(test2.Replace(":32B:", ""))
       Dim TestDisplay3 As String = GetNumbersfromStringUsingSB(B32Stinrg2.Replace(":32B:", ""))


       MessageBox.Show("The numerical value for this :32B:JPY12561694,60 is " & TestDisplay1, "", MessageBoxButtons.OK)
       MessageBox.Show("The numerical value for this :32B:JPY12532694, is " & TestDisplay3, "", MessageBoxButtons.OK)
       MessageBox.Show("The numerical value for this :32B:JPY12561694,:77CDX is " & TestDisplay2, "", MessageBoxButtons.OK)

   End Sub

    Function GetNumbersfromStringUsingSB(ByVal thestring As String) As Long
       Dim sb As New System.Text.StringBuilder(thestring.Length)
       For Each ch As Char In thestring
           If Char.IsDigit(ch) Then
               sb.Append(ch)
           End If
       Next
       Return Long.Parse(sb.ToString)
   End Function
Posted
Updated 22-May-13 11:15am
v4
Comments
[no name] 22-May-13 0:58am    
I have a small doubt. All the time your amount should possess a 3 letter word or any 2 letter word or nothing else, clarify this one
ekipongi 22-May-13 1:01am    
Thanks Ramanjaneya002. It will have 3 letter word all the time but it would be good if the code can also check for any number of letters infront.
Rockstar_ 22-May-13 1:56am    
You can do like this, remove all alphabets from the string and replace comma with period.
ekipongi 22-May-13 1:59am    
Yes, that's what I'm trying to do but I don't know how and I'm stuck. If you can please assist me with the coding.

All you need to do is to use Regex Class[^] with pattern like this: \d*\d. More patterns you'll find here: http://www.regular-expressions.info/reference.html[^]
 
Share this answer
 
What is the purpose of B32Stinrg.Replace(":32B:", "")?

With that set aside, your "GetNumbersfromStringUsingSB" method just needs a few tweeks. First off I changed it to return a Decimal instead of an Int64; i assumed you want that fraction component based on what you described. Second, I added condition to handle then case of "," or ".".

VB
Function GetNumbersfromStringUsingSB(ByVal thestring As String) As Decimal
    Dim sb As New System.Text.StringBuilder(thestring.Length)
    For Each ch As Char In thestring
        If Char.IsDigit(ch) Then
            sb.Append(ch)
        ElseIf ch = "."c OrElse ch = ","c Then
            sb.Append("."c)
        End If
    Next
    Return Decimal.Parse(sb.ToString)
End Function
 
Share this answer
 
Comments
ekipongi 22-May-13 19:19pm    
Thanks TnTinMn for the assistance. Got it working.
TnTinMn 22-May-13 19:30pm    
Cool!. Time for the happy dance.
http://3.bp.blogspot.com/_Ty55Ed2OdlE/TPpTlPHJAzI/AAAAAAAAAQQ/tQkMY56I1co/s1600/chAnimated.gif

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900