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.
The decimal value obtained from s.
Type Reason ArgumentNullException s is a null reference. FormatException s is not in the correct format. 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 , System.Globalization.NumberStyles.Number , 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. Any scale apparent in the string s is preserved unless the value is rounded. If the value is zero, the sign 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 the decimal.Parse(string) method.
C# Example
using System; using System.Globalization; class DecimalParseClass { public static void Main() { string s1 = " -1.001 "; string s2 = "+1,000,111.99"; string s3 = "2.900"; Console.WriteLine("String: {0} (decimal) {1}",s1,Decimal.Parse(s1)); Console.WriteLine("String: {0} (decimal) {1}",s2,Decimal.Parse(s2)); Console.WriteLine("String: {0} (decimal) {1}",s3,Decimal.Parse(s3)); } }The output is
String: -1.001 (decimal) -1.001
String: +1,000,111.99 (decimal) 1000111.99
String: 2.900 (decimal) 2.900
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0