Share via


Parsing Numeric Strings

All numeric types have a static Parse method that you can use to convert a string representation of a numeric type into an actual numeric type. These methods allow you to parse strings that were produced using one of the formatting specifiers covered in the section, Formatting Types.

The characters used to represent currency symbols, thousand separators, and decimal points are defined in format providers. The Parse method accepts a format provider, allowing you to specify and explicitly parse culture-specific strings. If no format provider is specified, then the provider associated with the current thread is used. For more information, see Formatting Overview.

The following code example converts a string to an integer value, increments that value, and displays the result.

Dim MyString As String = "12345"
Dim MyInt As Integer = Integer.Parse(MyString)
MyInt += 1
Console.WriteLine(MyInt)
' The result is "12346".
[C#]
string MyString = "12345";
int MyInt = int.Parse(MyString);
MyInt++;
Console.WriteLine(MyInt); 
// The result is "12346". 

The NumberStyles enumeration is useful if you have a string that contains nonnumeric characters that you want converted into a .NET Framework numeric base type. You can use this enumeration to parse a string that contains a currency symbol, a decimal point, an exponent, parentheses, and so on. For example, in the en-US culture, a string that contains a comma cannot be converted to an integer value using the Parse method if the NumberStyles.AllowThousands enumeration is not passed.

NumberStyles.AllowCurrencySymbol specifies that a number should be parsed as a currency rather than as a decimal. NumberStyles.AllowDecimalPoint indicates that a decimal point is allowed. Valid decimal point characters are determined by the NumberDecimalSeparator or CurrencyDecimalSeparator properties of the current NumberFormatInfo object. NumberStyles.AllowThousands indicates that group separators are allowed. Valid group separator characters are determined by the NumberGroupSeparator or CurrencyGroupSeparator properties of the current NumberFormatInfo object. For a complete table of valid nonnumeric character types, see the NumberStyles enumeration documentation.

The NumberStyles enumeration uses the characters specified by the current culture to aid in the parse. If you do not specify a culture by passing a CultureInfo object set to the culture that corresponds to the string you are parsing, the culture associated with the current thread is used.

The following code example is invalid and will raise an exception. It illustrates the improper way to parse a string containing nonnumeric characters. A new CultureInfo is first created and passed to the Parse method to specify that the en-US culture be used for parsing.

Imports System.Globalization

Dim MyCultureInfo As CultureInfo = new CultureInfo("en-US")
Dim MyString As String = "123,456"
Dim MyInt As Integer = Integer.Parse(MyString, MyCultureInfo)
Console.WriteLine(MyInt)
' Raises System.Format exception.
[C#]
using System.Globalization;

CultureInfo MyCultureInfo = new CultureInfo("en-US");
string MyString = "123,456";
int MyInt = int.Parse(MyString, MyCultureInfo);
Console.WriteLine(MyInt); 
// Raises System.Format exception.

When you apply the NumberStyles enumeration with the AllowThousands flag, the Parse method ignores the comma that raised the exception in the previous example. The following code example uses the same string as the previous example, but does not raise an exception. Similar to the previous example, a new CultureInfo is first created and passed to the Parse method to specify that the thousand separator used by the en-US culture is used for parsing.

Imports System.Globalization

Dim MyCultureInfo As CultureInfo = new CultureInfo("en-US")
Dim MyString As String = "123,456"
Dim MyInt As Integer = Integer.Parse(MyString, NumberStyles.AllowThousands, MyCultureInfo)
Console.WriteLine(MyInt)
' The result is "123456".
[C#]
using System.Globalization;

CultureInfo MyCultureInfo = new CultureInfo("en-US");
string MyString = "123,456";
int MyInt = int.Parse(MyString, NumberStyles.AllowThousands, MyCultureInfo);
Console.WriteLine(MyInt); 
// The result is "123456".

See Also

Parsing Strings | Formatting Types | Converting Types | NumberStyles Enumeration