Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 NumberStyles Enumeration
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
NumberStyles Enumeration

Determines the styles permitted in numeric string arguments that are passed to the Parse methods of the numeric base type classes.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<SerializableAttribute> _
<FlagsAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration NumberStyles
Visual Basic (Usage)
Dim instance As NumberStyles
C#
[SerializableAttribute]
[FlagsAttribute]
[ComVisibleAttribute(true)]
public enum NumberStyles
Visual C++
[SerializableAttribute]
[FlagsAttribute]
[ComVisibleAttribute(true)]
public enum class NumberStyles
JScript
public enum NumberStyles
Member nameDescription
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkNoneIndicates that none of the bit styles are allowed.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkAllowLeadingWhiteIndicates that a leading white-space character must be ignored during parsing. Valid white-space characters have the Unicode values U+0009, U+000A, U+000B, U+000C, U+000D, and U+0020.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkAllowTrailingWhiteIndicates that trailing white-space character must be ignored during parsing. Valid white-space characters have the Unicode values U+0009, U+000A, U+000B, U+000C, U+000D, and U+0020.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkAllowLeadingSignIndicates that the numeric string can have a leading sign. Valid leading sign characters are determined by the PositiveSign and NegativeSign properties of NumberFormatInfo.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkAllowTrailingSignIndicates that the numeric string can have a trailing sign. Valid trailing sign characters are determined by the PositiveSign and NegativeSign properties of NumberFormatInfo.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkAllowParenthesesIndicates that the numeric string can have one pair of parentheses enclosing the number.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkAllowDecimalPointIndicates that the numeric string can have a decimal point. Valid decimal point characters are determined by the NumberDecimalSeparator and CurrencyDecimalSeparator properties of NumberFormatInfo.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkAllowThousandsIndicates that the numeric string can have group separators, for example, separating the hundreds from the thousands. Valid group separator characters are determined by the NumberGroupSeparator and CurrencyGroupSeparator properties of NumberFormatInfo and the number of digits in each group is determined by the NumberGroupSizes and CurrencyGroupSizes properties of NumberFormatInfo.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkAllowExponentIndicates that the numeric string can be in exponential notation.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkAllowCurrencySymbolIndicates that the numeric string is parsed as currency if it contains a currency symbol. Otherwise, it is parsed as a number. Valid currency symbols are determined by the CurrencySymbol property of NumberFormatInfo.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkAllowHexSpecifierIndicates that the numeric string represents a hexadecimal value. Valid hexadecimal values include the numeric digits 0-9 and the hexadecimal digits A-F and a-f. Hexadecimal values can be left-padded with zeros. Strings parsed using this style are not permitted to be prefixed with "0x".
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkIntegerIndicates that the AllowLeadingWhite, AllowTrailingWhite, and AllowLeadingSign styles are used. This is a composite number style.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkHexNumberIndicates that the AllowLeadingWhite, AllowTrailingWhite, and AllowHexSpecifier styles are used. This is a composite number style.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkNumberIndicates that the AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign, AllowTrailingSign, AllowDecimalPoint, and AllowThousands styles are used. This is a composite number style.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkFloatIndicates that the AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign, AllowDecimalPoint, and AllowExponent styles are used. This is a composite number style.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkCurrencyIndicates that all styles, except AllowExponent and AllowHexSpecifier, are used. This is a composite number style.
Supported by the .NET Compact FrameworkSupported by the XNA FrameworkAnyIndicates that all styles, except AllowHexSpecifier, are used. This is a composite number style.

The symbols to use for currency symbol, thousands separator, decimal point indicator, and leading sign are specified by NumberFormatInfo.

The AllowCurrencySymbol and AllowDecimalPoint values determine whether a Parse method treats an instance of a numeric type as a currency or as a number and, therefore, whether to use the currency properties or the number properties of the associated NumberFormatInfo. For example, if AllowCurrencySymbol is specified, a Parse method uses currency properties, such as CurrencyDecimalSeparator and CurrencyGroupSeparator. However, if AllowDecimalPoint is specified, the method uses number properties, such as NumberDecimalSeparator and NumberGroupSeparator.

The attributes of NumberStyles are set by using the bitwise inclusive OR of the field flags. The Most Significant Bit (MSB) is the leftmost bit.

In the following table, the rows are the number styles with individual field flags and the columns are the composite number styles that combine individual field flags.

Under the composite number styles, a "1" indicates that the composite number style includes the individual number style in that row. A "0" indicates that the composite number style does not include the individual number style in that row.

The hexadecimal representation of the composite number styles are listed in the last row of the table.

Any

Currency

Float

Integer

Number

HexNumber

AllowHexSpecifier (0x0200)

0

0

0

0

0

1

AllowCurrencySymbol (0x0100)

1

1

0

0

0

0

AllowExponent (0x0080)

1

0

1

0

0

0

AllowThousands (0x0040)

1

1

0

0

1

0

AllowDecimalPoint (0x0020)

1

1

1

0

1

0

AllowParentheses (0x0010)

1

1

0

0

0

0

AllowTrailingSign (0x0008)

1

1

0

0

1

0

AllowLeadingSign (0x0004)

1

1

1

1

1

0

AllowTrailingWhite (0x0002)

1

1

1

1

1

1

AllowLeadingWhite (0x0001)

1

1

1

1

1

1

(0x1ff)

(0x17f)

(0x0a7)

(0x007)

(0x06f)

(0x203)

The AllowExponent style is used for numeric strings in one of the following forms:

[-] m. dddddd E+ xx

[-] m. dddddd E- xx

[-] m. dddddd e+ xx

[-] m. dddddd e- xx

One or more nonzero digits (m) precede the decimal separator ("."). A minus sign ("-") can precede m. The Type performing the conversion determines the number of decimal places (dddddd) in the string, and maximum and minimum values for xx and m. The exponent (+ / - xx) consists of either a plus or minus sign followed by at least one digit.

This example shows how to parse a string into a 32-bit integer using various NumberStyles flags.

C#
using System;
using System.Text;
using System.Globalization;

public sealed class App 
{
    static void Main() 
    {
        // Parse the string as a hex value and display the value as a decimal.
        String num = "A";
        int val = int.Parse(num, NumberStyles.HexNumber);
        Console.WriteLine("{0} in hex = {1} in decimal.", num, val);

        // Parse the string, allowing a leading sign, and ignoring leading and trailing white spaces.
        num = "    -45   ";
        val = int.Parse(num, NumberStyles.AllowLeadingSign | 
            NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite);
        Console.WriteLine("'{0}' parsed to an int is '{1}'.", num, val);

        // Parse the string, allowing parentheses, and ignoring leading and trailing white spaces.
        num = "    (37)   ";
        val = int.Parse(num, NumberStyles.AllowParentheses | NumberStyles.AllowLeadingSign |                         NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite);
        Console.WriteLine("'{0}' parsed to an int is '{1}'.", num, val);
    }
}

// This code produces the following output.
//
// A in hex = 10 in decimal.
// '    -45   ' parsed to an int is '-45'.
// '    (37)   ' parsed to an int is '-37'.
Visual C++
using namespace System;
using namespace System::Text;
using namespace System::Globalization;


int main()
{
    // Parse the string as a hex value and display the
    // value as a decimal.
    String^ numberString = "A";
    int stringValue = Int32::Parse(numberString, NumberStyles::HexNumber);
    Console::WriteLine("{0} in hex = {1} in decimal.",
        numberString, stringValue);

    // Parse the string, allowing a leading sign, and ignoring
    // leading and trailing white spaces.
    numberString = "    -45   ";
    stringValue =Int32::Parse(numberString, NumberStyles::AllowLeadingSign |
        NumberStyles::AllowLeadingWhite | NumberStyles::AllowTrailingWhite);
    Console::WriteLine("'{0}' parsed to an int is '{1}'.",
        numberString, stringValue);

    // Parse the string, allowing parentheses, and ignoring
    // leading and trailing white spaces.
    numberString = "    (37)   ";
    stringValue = Int32::Parse(numberString, NumberStyles::AllowParentheses |
        NumberStyles::AllowLeadingSign | NumberStyles::AllowLeadingWhite |
        NumberStyles::AllowTrailingWhite);

    Console::WriteLine("'{0}' parsed to an int is '{1}'.",
        numberString, stringValue);
}

// This code produces the following output.
//
// A in hex = 10 in decimal.
// '    -45   ' parsed to an int is '-45'.
// '    (37)   ' parsed to an int is '-37'.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Parse string using PowerShell      Thomas Lee   |   Edit   |   Show History
# Get-ParsedString2.ps1
# MSDN Sample recoded using PowerShell
# See: http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles(VS.85).aspx
# This script usesd the .value__ to obtain the value of each enum
# Thomas Lee - tfl@psp.co.uk






# Parse the string as a hex value and display the value as a decimal.
$num = "A"
$val = [system.int32]::Parse($num, [System.Globalization.NumberStyles]::HexNumber)
"{0} in hex = {1} in decimal" -f $num,$val
  
# Parse the string, allowing a leading sign, and ignoring leading and trailing white spaces.
$num = " -45 "
  
# create parsing value using value__
$parsing = [System.Globalization.NumberStyles]::AllowLeadingSign.value__+
[System.Globalization.NumberStyles]::AllowLeadingWhite.value__ +
[System.Globalization.NumberStyles]::AllowTrailingWhite.value__ 
  
# parse and display
$val = [system.int32]::Parse($num,$parsing)
"'{0}' parsed to an int32 is '{1}'." -f $num, $val
# Now try a negative number
  
$num = " (37) "
$parsing = [System.Globalization.NumberStyles]::AllowParentheses.value__ +
[System.Globalization.NumberStyles]::AllowLeadingSign.value__ +
[System.Globalization.NumberStyles]::AllowLeadingWhite.value__ +
[System.Globalization.NumberStyles]::AllowTrailingWhite.value__
  
# And parse/display it
$val = [system.int32]::Parse($num, $parsing)
"'{0}' parsed to an int32 is '{1}'." -f $num, $val


This script produces the following output:

PSH [D:\foo]: .\Get-ParsedString2.ps1'
A in hex = 10 in decimal
' -45 ' parsed to an int32 is '-45'.
' (37) ' parsed to an int32 is '-37'.
  
AllowExponent does not account for all elements of exponential notation      jan-joost   |   Edit   |   Show History
Tags What's this?: Add a tag
Flag as ContentBug
AllowExponent only E+/-xx element      jan-joost   |   Edit   |   Show History
Tags What's this?: Add a tag
Flag as ContentBug
AllowExponent only E+/-xx element      jan-joost   |   Edit   |   Show History
AllowExponent only acounts for the exponent element      jan-joost   |   Edit   |   Show History

In the article it looks like using AllowExponent accounts for a complete exponential notation, including decimal point, sign and everything. However, when you want to allow the other elements of the exponential notation, you have to include things like AllowDecimalPoint and AllowLeadingSign or use the Float number style.

The following will throw a FormatException:

String tString = "-13.02E-01";
Double tDouble = Double.Parse(tString, NumberStyles.AllowExponent);
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker