Returns the specified string converted to a decimal value.
- 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.
The decimal value obtained from s .
Type Reason ArgumentNullException s is a null reference. FormatException s is not in the correct style. OverflowException s represents a number greater than decimal.MaxValue or less than decimal.MinValue.
This version of decimal.Parse(string) is equivalent to decimal.Parse(string) (s, style , null ).
The string s is parsed using the formatting information in a System.Globalization.NumberFormatInfo initialized for the current system culture.
Note: For more information, see System.Globalization.NumberFormatInfo.CurrentInfo .If necessary, the value of s is rounded using banker's rounding.
The following example demonstrates supplying System.Globalization.NumberStyles values to the decimal.Parse(string) method to allow for a symbol separating groups of digits, and a decimal separator. This example uses the symbols from the U.S. English culture, namely a comma 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; decimal d = Decimal.Parse(s,ns); 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