Mono Class Library: System.Decimal Overview | Members

System.Decimal.Parse Method

Returns the specified string converted to a decimal value.

public static decimal Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider)

Parameters

s
A string containing the value to convert. The string is interpreted using the style specified by style , preserving scale.
style
Zero or more System.Globalization.NumberStyles values that specify the style of s. Specify multiple values for style using the bitwise OR operator. If style is a null reference, the string is interpreted using the System.Globalization.NumberStyles.Number style.
provider
A IFormatProvider that supplies a System.Globalization.NumberFormatInfo containing culture-specific formatting information about s.

Returns

The decimal value obtained from s .

Exceptions

TypeReason
ArgumentNullExceptions is a null reference.
FormatExceptions is not in the correct style.
OverflowExceptions represents a number greater than decimal.MaxValue or less than decimal.MinValue.

Remarks

The string s is parsed using the culture-specific formatting information from the System.Globalization.NumberFormatInfo instance supplied by provider. If provider is null or if a System.Globalization.NumberFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.

If necessary, the value of s is rounded using banker's rounding.

Example

The following example demonstrates supplying System.Globalization.NumberStyles values and a IFormatProvider to the decimal.Parse(string) method to allow colons separating groups of digits, and a decimal point.

C# Example

using System;
using System.Globalization;
class DecimalParseClass {
public static void Main() {
 string s = "1:000:111.99"; 
 NumberStyles ns = NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint;
 NumberFormatInfo nfi = new NumberFormatInfo();
 //Change the format info to separate digit groups using a colon.
 nfi.NumberGroupSeparator = ":";
 decimal d = Decimal.Parse(s,ns,nfi);
 Console.WriteLine("{0} parsed to decimal {1}",s,d);
}
}

The output is

1:000:111.99 parsed to decimal 1000111.99

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0