Returns the specified string converted to a decimal value.
- s
- A string containing the value to convert. The string is interpreted using the System.Globalization.NumberStyles.Number style, preserving scale.
- provider
- A IFormatProvider that supplies a System.Globalization.NumberFormatInfo containing culture-specific formatting information about s.
The decimal value obtained from s .
Type Reason FormatException s is not in the correct style. OverflowException s represents a number greater than decimal.MaxValue or less than decimal.MinValue. ArgumentNullException s is a null reference.
This version of decimal.Parse(string) is equivalent to decimal.Parse(string) (s, System.Globalization.NumberStyles.Number , provider ).
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 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. Any scale apparent in the string s is preserved unless the value is rounded. If the value is zero, the sign scale will be 0. Hence the string "2.900" will be parsed to form the decimal with sign 0, coefficient 2900, and scale 3.
The following example demonstrates supplying a IFormatProvider to the decimal.Parse(string) method to allow a decimal point, and commas separating groups of digits.
C# Example
using System; using System.Globalization; class DecimalParseClass { public static void Main() { string s = "1,000,111.99"; //Get the default formatting symbols. NumberFormatInfo nfi = new NumberFormatInfo(); // Default group separator is ',' // Default decimal separator is '.' decimal d = Decimal.Parse(s,nfi); Console.WriteLine("{0} parsed to decimal {1}",s,d); } }The output is
1,000,111.99 parsed to decimal 1000111.99
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0