Convert a string to decimal in VB.NET

94,445

Solution 1

Use Decimal.Parse to convert to decimal number, and then use .ToString("format here") to convert back to a string.

Dim aAsDecimal as Decimal = Decimal.Parse(a).ToString("format here")

Last resort approach (not recommended):

string s = (aAsDecimal <0) ? Math.Abs(aAsDecimal).ToString("##,###0.00") + "-" : aAsDecimal .ToString("##,###0.00");

You will have to translate to Visual Basic.

Solution 2

For VB.NET:

CDec(Val(string_value))

For example,

CDec(Val(a))

The result will be 40000D or if the value for a = "400.02" then it will be 400.02D.

Solution 3

Use Decimal.TryParse

Dim a as string
Dim b as Decimal
If Decimal.TryParse(a, b) Then
   a = b.ToString("##,###.00")
Else
   a = "can not parse"
End If

Solution 4

Sub Main()
    Dim convert As Func(Of String, Decimal) = _
    Function(x As String) Decimal.Parse(x) ' This is a lambda expression.
    Dim a = convert("-16325.62")
    Dim spec As String = "N"
    Console.WriteLine("{1}", spec, a.ToString(spec))
    'Console.ReadLine() ' Uncomment to see value in Console output.
End Sub

Solution 5

The following works fine for me, but I don't know whether it is correct or not.

double a = 40000.00;
a = double.Parse(a.ToString("##,###.00"));
MessageBox.Show(a.ToString("##,###.00"));
Share:
94,445
user709787
Author by

user709787

Updated on July 09, 2022

Comments

  • user709787
    user709787 almost 2 years

    What will be the easiest way to convert a string to decimal?

    Input:

    a = 40000.00-
    

    Output will be

    40,000.00-
    

    I tried to use this code:

    Dim a as string
    
    a = "4000.00-"
    
    a = Format$(a, "#,###.##")
    console.writeline (a)
    
  • user709787
    user709787 almost 13 years
    I try this but the result i want is negative should be at the end. Dim a as string a = "4000.00-" a = Decimal.Parse(a).ToString("##,###.00") console.writeline (a) ' result 4,000.00
  • Slappy
    Slappy almost 13 years
    Else there is the unrecommended approach: (a<0) ? Math.Abs(a).ToString("##,###0.00") + "-" : a.ToString("##,###0.00");
  • user709787
    user709787 almost 13 years
    still no luck. I'll try it in a different way. I'll post it here once I got it. Thanks for your reply.
  • Slappy
    Slappy almost 13 years
    My last resort approach will def work. Though not entirely elegant.
  • user709787
    user709787 almost 13 years
    When I convert to VB I got an error Dim s As String = If((a < 0), Math.Abs(a).ToString("##,###0.00") & "-", a.ToString("##,###0.00"))
  • Slappy
    Slappy almost 13 years
    Dim aAsDecimal as Decimal = Decimal.Parse(a) then use aAsDecimal in the statement I gave you.
  • user709787
    user709787 almost 13 years
    this is the website I used to convert your code developerfusion.com/tools/convert/csharp-to-vb
  • Slappy
    Slappy almost 13 years
    My original used a which is defined as a string. Which is why I introduced aAsADecimal. You probably getting a typecast exception or something.
  • dwilliss
    dwilliss over 9 years
    middle line is unnecessary. You're converting a double to a string and then parsing it again back to double. Now, if you're trying to truncate extra decimal places (ie: 40000.0001 to 40000.00), that will work, but Math.Round(a, 2) will be more efficient.