• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

Format a string which is in the form of $10,000 to double


I have the following code, I'm passing a strings of the format $12,000. I need to convert them to double,
using CDBL  function is not giving me the desired result.
I need to convert sLineAmt and sCrLineamt to double and then divide sLIneAmt/sCrLineAmt and multiply the result by 100 and then convert the entire result back to string and append a "%" .

Please help em to get the result.
It's extremely urgent. I have  adeadline to meet in another 2 hrs.
Thanks a million in advance.

Private Function Get_Cash_Line_Option_Set_Desc_Percent(ByVal sLineOptSet As String, ByVal sLineAmt As String, ByVal sCrLineAmt As String) As String

        Dim osqlAdapter As New SqlDataAdapter
        Dim oDs As New DataSet
        Dim sDesc As String = String.Empty
        Dim dCreditLineAmt As Double = 0
        Dim dCashLineAmt As Double = 0
        'Dim sOptSet As String() = Nothing
        'Dim strOptSet As String = String.Empty


            oDs = XML.getxml()
            If Not oDs Is Nothing Then
                If oDs.Tables(0).Rows.Count > 0 Then
                    For Each odr As DataRow In oDs.Tables(0).Rows
                        If Not sCashLineOptSet Is Nothing Then
                            If sCashLineOptSet.Length > 1 Then
                                If (odr("OPTION_SET") = sOptSet) Then
                                    'sDesc = odr("option_set_desc")
                                    If (odr("OPTION_SET") <> "n/a" And odr("OPTION_SET").Contains("/")) Then
                                                                               sDesc = (CDbl(sLineAmt) / CDbl(sCrLineAmt)).ToString
                                        sDesc = sDesc & "%"
                                        sDesc = odr("option_set_desc")

                                    End If

                                End If
                            End If
                        End If

                End If

                sDesc = "n/a"
            End If

                           Return sDesc
        Catch ex As Exception
            Throw ex
        End Try
    End Function
  • 2
2 Solutions

doubleVal = System.Convert.ToDouble(stringVal)
I also think that you need to first strip off the $ sign before converting to Double.
You should be doing something like

dim sValue as string = "$ 48,000"
dim sModifiedValue as string= sValue.Replace("$","").Replace(",","").Trim()
dim dblValue as double = Convert.ToDouble(sModifiedValue)

Tony McCreathTechnical SEO ConsultantCommented:
This should work.

I've converted it from C# so I might have not got the syntax perfect.
dim newPrice as double = 0.0
dim valid as bool =  Double.TryParse("$12,000",System.Globalization.NumberStyles.Currency,null,out newPrice)

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now