NumberFormatInfo.NumberGroupSizes Property

Gets or sets the number of digits in each group to the left of the decimal in numeric values.

Namespace:  System.Globalization
Assemblies:   mscorlib (in mscorlib.dll)
  System.Globalization (in System.Globalization.dll)

'Declaration
Public Property NumberGroupSizes As Integer()

Property Value

Type: System.Int32()
The number of digits in each group to the left of the decimal in numeric values. The default for InvariantInfo is a one-dimensional array with only one element, which is set to 3.

ExceptionCondition
ArgumentNullException

The property is being set to Nothing.

ArgumentException

The property is being set and the array contains an entry that is less than 0 or greater than 9.

-or-

The property is being set and the array contains an entry, other than the last entry, that is set to 0.

InvalidOperationException

The property is being set and the NumberFormatInfo object is read-only.

The value of the NumberGroupSizes property affects the result of number values that are formatted by using the "N" standard numeric format string. If a custom numeric format string or other standard numeric format strings are used, the value of the NumberGroupSizes property is ignored.

Every element in the one-dimensional array must be an integer from 1 through 9. The last element can be 0.

The first element of the array defines the number of elements in the least significant group of digits immediately to the left of the NumberDecimalSeparator. Each subsequent element refers to the next significant group of digits to the left of the previous group. If the last element of the array is not 0, the remaining digits are grouped based on the last element of the array. If the last element is 0, the remaining digits are not grouped.

For example, if the array contains { 3, 4, 5 }, the digits are grouped similar to "55,55555,55555,55555,4444,333.00". If the array contains { 3, 4, 0 }, the digits are grouped similar to "55555555555555555,4444,333.00".

The following example demonstrates the effect of changing the NumberGroupSizes property.

Imports System
Imports System.Globalization

Class NumberFormatInfoSample

   Public Shared Sub Main()

      ' Gets a NumberFormatInfo associated with the en-US culture. 
      Dim nfi As NumberFormatInfo = New CultureInfo("en-US", False).NumberFormat

      ' Displays a value with the default separator ("."). 
      Dim myInt As Int64 = 123456789012345
      Console.WriteLine(myInt.ToString("N", nfi))

      ' Displays the same value with different groupings. 
      Dim mySizes1 As Integer() =  {2, 3, 4}
      Dim mySizes2 As Integer() =  {2, 3, 0}
      nfi.NumberGroupSizes = mySizes1
      Console.WriteLine(myInt.ToString("N", nfi))
      nfi.NumberGroupSizes = mySizes2
      Console.WriteLine(myInt.ToString("N", nfi))

   End Sub 'Main 

End Class 'NumberFormatInfoSample



'This code produces the following output. 



'123,456,789,012,345.00 

'12,3456,7890,123,45.00 

'1234567890,123,45.00

The following example prints a value using different NumberGroupSizes arrays.

Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Class SamplesNumberFormatInfo

    Public Shared Sub Main()

        ' Creates a new NumberFormatinfo. 
        Dim myNfi As New NumberFormatInfo()

        ' Takes a long value. 
        Dim myInt As Int64 = 123456789012345

        ' Displays the value with default formatting.
        Console.WriteLine("Default  " + ControlChars.Tab + ControlChars.Tab _
           + ":" + ControlChars.Tab + "{0}", myInt.ToString("N", myNfi))

        ' Displays the value with three elements in the GroupSize array.
        myNfi.NumberGroupSizes = New Integer() {2, 3, 4}
        Console.WriteLine("Grouping ( {0} )" + ControlChars.Tab + ":" _
           + ControlChars.Tab + "{1}", _
           PrintArraySet(myNfi.NumberGroupSizes), myInt.ToString("N", myNfi))

        ' Displays the value with zero as the last element in the GroupSize array.
        myNfi.NumberGroupSizes = New Integer() {2, 4, 0}
        Console.WriteLine("Grouping ( {0} )" + ControlChars.Tab + ":" _
           + ControlChars.Tab + "{1}", _
           PrintArraySet(myNfi.NumberGroupSizes), myInt.ToString("N", myNfi))
    End Sub 


    Public Shared Function PrintArraySet(myArr() As Integer) As String 
        Dim myStr As String = Nothing
        myStr = myArr(0).ToString()
        Dim i As Integer 
        For i = 1 To myArr.Length - 1
            myStr += ", " + myArr(i).ToString()
        Next i
        Return myStr
    End Function 
End Class 

' This code produces the following output.  
'  
' Default                 :       123,456,789,012,345.00 
' Grouping ( 2, 3, 4 )    :       12,3456,7890,123,45.00 
' Grouping ( 2, 4, 0 )    :       123456789,0123,45.00 

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft