Try to parse string to decimal in CSharp

Description

The following code shows how to try to parse string to decimal.

Example


using System;/*from   ww w.  j  av a2  s . c  o  m*/

class MainClass
{
    public static void Main( )
    {
        string value;
        decimal number;
        
        // Parse a floating-point value with a thousands separator.
        value = "1,234.56";
        if (Decimal.TryParse(value, out number))
           Console.WriteLine(number);
        else
           Console.WriteLine("Unable to parse '{0}'.", value);      
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var