BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# String to Number Conversion

The eighteenth part of the C# Fundamentals tutorial brings together the native data types discussed so far. This article describes how to convert string values to numeric types. This is essential when allowing free-form user input of numeric data.

Using Number Styles

The simple manner of calling Parse described above has a drawback. The string containing the number must contain only numeric digits. A number such as "£10,000" causes an exception when parsed. To avoid this, a second parameter can be added to define the permitted number styles for the string's contents. The .NET framework provides an enumeration for allowable number styles. An enumeration is a special value type that contains a named set of available values. Enumerations will be described in detail later in the tutorial.

The NumberStyles enumeration is defined within the System.Globalization namespace. To avoid having to type System.Globalization before every reference to NumberStyles, add a using directive to the start of the code file.

using System.Globalization;

With the using directive added, we can use the NumberStyles enumeration as a second parameter to the Parse method.

// Conversion including thousands separator
int thousands = int.Parse("1,000", NumberStyles.AllowThousands);

// Conversion of value including a decimal point
float withDecimal = float.Parse("5.50", NumberStyles.AllowDecimalPoint);

// Conversion of value including a currency symbol
// (Make sure you change the currency symbol to match your local currency)
int withCurrency = int.Parse("£5", NumberStyles.AllowCurrencySymbol);

The NumberStyles enumeration values can be combined using the OR operator (|), allowing multiple styles. For example:

// Conversion of value including a decimal point and currency symbol
float multiple = float.Parse(
    "£5.50", NumberStyles.AllowDecimalPoint | NumberStyles.AllowCurrencySymbol);

The enumeration provides some predefined styles. The example above used two styles to convert a currency value. We can use the Currency option from the NumberStyles enumeration to permit these styles and more.

// Conversion of a currency value
float money = float.Parse("£9,999.99", NumberStyles.Currency);

Available Number Styles

The full list of available number styles is as follows:

Number StylesDescriptionExample
AllowCurrencySymbolThe string may include a currency symbol. The currency symbol permitted is determined by the user's local configuration settings."£1500"
AllowDecimalPointThe string may include a single decimal point"1500.00"
AllowExponentThe string may be a number expressed using exponential notation."1.5E3"
AllowHexSpecifierThe string may contain a hexadecimal number only."5DC"
AllowLeadingSignThe string may include a leading sign indicating a positive or negative number. The sign symbols permitted are determined by the user's local configuration settings."-1500"
AllowLeadingWhiteThe string may include leading whitespace characters including true spaces and some control characters (Unicode characters 9 to 13)." 1500"
AllowParenthesesThe string may include a single pair of parentheses around the number. If the parentheses are present, the number is determined to be negative."(1500)"
AllowThousandsThe string may include thousands separators. The separator symbol permitted is determined by the user's local configuration settings."1,500"
AllowTrailingSignThe string may include a trailing sign indicating a positive or negative number. The sign symbols permitted are determined by the user's local configuration settings."1500-"
AllowTrailingWhiteThe string may include trailing whitespace characters."1500 "
AnyThe string may contain any combination of the above "Allow" number styles except for the use of hexadecimal numbers."1.5E3-"
CurrencyThe string may contain any combination of the above "Allow" number styles except for the use of hexadecimal numbers or exponential notation."(£1,500.00)"
FloatProvides a combination of the AllowDecimalPoint, AllowExponent, AllowLeadingSign, AllowLeadingWhite and AllowTrailingWhite number styles."-1,500.00"
HexNumberProvides a combination of the AllowHexSpecifier, AllowLeadingWhite and AllowTrailingWhite number styles." 5DC "
IntegerProvides a combination of the AllowLeadingSign, AllowLeadingWhite and AllowTrailingWhite number styles." -1500"
NoneThe string may include none of the "Allow" number styles."1500"
NumberProvides a combination of the AllowDecimalPoint, AllowLeadingSign, AllowLeadingWhite, AllowThousands, AllowTrailingSign and AllowTrailingWhite number styles." -1,500.00 "

Internationalisation

The .NET framework includes a large amount of functionality relating to internationalisation (US:internationalization). This simply means developing your software to function for users around the world using their local settings for numbers, currencies, languages, etc. This is a complex subject and beyond the scope of a tutorial for C# language basics. Generally speaking, for numeric conversions you can use the above number styles to use local settings.

11 November 2006