Parse string using "$" as the currency symbol to decimal in CSharp

Description

The following code shows how to parse string using "$" as the currency symbol to decimal.

Example


using System;/*from   w ww .  j  a v a2 s  . co  m*/
using System.Globalization;
class MainClass
{
    public static void Main()
    {
        string value = "$6,543.21";
        NumberStyles style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
        CultureInfo provider = new CultureInfo("en-GB");

        try
        {
            decimal number = Decimal.Parse(value, style, provider);
            Console.WriteLine("'{0}' converted to {1}.", value, number);
        }
        catch (FormatException)
        {
            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