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)

Parameters

s
A string containing the value to convert. The string is interpreted using the System.Globalization.NumberStyles.Number style, preserving scale.

Returns

The decimal value obtained from s.

Exceptions

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

Remarks

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.

Example

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

Requirements

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