C#idiomatic C#
standard-numeric-format-stringsstandard/base-types

Core library

Standard numeric format strings

In this article, learn to use standard numeric format strings to format common numeric types into text representations in .NET.

  • page standard/base-types/standard-numeric-format-strings
  • updated 10/22/2024

Standard numeric format strings are used to format common numeric types. A standard numeric format string takes the form [format specifier][precision specifier], where:

  • Format specifier is a single alphabetic character that specifies the type of number format, for example, currency or percent. Any numeric format string that contains more than one alphabetic character, including white space, is interpreted as a custom numeric format string. For more information, see Custom numeric format strings.

  • Precision specifier is an optional integer that affects the number of digits in the resulting string. In .NET 7 and later versions, the maximum precision value is 999,999,999. In .NET 6, the maximum precision value is System.Int32.MaxValue. In previous .NET versions, the precision can range from 0 to 99. The precision specifier controls the number of digits in the string representation of a number. It does not round the number itself. To perform a rounding operation, use the System.Math.Ceiling, System.Math.Floor, or System.Math.Round method.

    When precision specifier controls the number of fractional digits in the result string, the result string reflects a number that is rounded to a representable result nearest to the infinitely precise result. If there are two equally near representable results:

Standard numeric format strings are supported by:

Standard format specifiers

The following table describes the standard numeric format specifiers and displays sample output produced by each format specifier. See the Notes section for additional information about using standard numeric format strings, and the Code example section for a comprehensive illustration of their use.

The result of a formatted string for a specific culture might differ from the following examples. Operating system settings, user settings, environment variables, and the .NET version you're using can all affect the format. For example, starting with .NET 5, .NET tries to unify cultural formats across platforms. For more information, see .NET globalization and ICU.

Format specifier Name Description Examples
"B" or "b" Binary Result: A binary string.

Supported by: Integral types only (.NET 8+).

Precision specifier: Number of digits in the result string.

More information: The Binary ("B") Format Specifier.
42 ("B")
-> 101010

255 ("b16")
-> 0000000011111111
"C" or "c" Currency Result: A currency value.

Supported by: All numeric types.

Precision specifier: Number of decimal digits.

Default precision specifier: Defined by System.Globalization.NumberFormatInfo.CurrencyDecimalDigits.

More information: The Currency ("C") Format Specifier.
123.456 ("C", en-US)
-> \$123.46

123.456 ("C", fr-FR)
-> 123,46 €

123.456 ("C", ja-JP)
-> ¥123

-123.456 ("C3", en-US)
-> (\$123.456)

-123.456 ("C3", fr-FR)
-> -123,456 €

-123.456 ("C3", ja-JP)
-> -¥123.456
"D" or "d" Decimal Result: Integer digits with optional negative sign.

Supported by: Integral types only.

Precision specifier: Minimum number of digits.

Default precision specifier: Minimum number of digits required.

More information: The Decimal("D") Format Specifier.
1234 ("D")
-> 1234

-1234 ("D6")
-> -001234
"E" or "e" Exponential (scientific) Result: Exponential notation.

Supported by: All numeric types.

Precision specifier: Number of decimal digits.

Default precision specifier: 6.

More information: The Exponential ("E") Format Specifier.
1052.0329112756 ("E", en-US)
-> 1.052033E+003

1052.0329112756 ("e", fr-FR)
-> 1,052033e+003

-1052.0329112756 ("e2", en-US)
-> -1.05e+003

-1052.0329112756 ("E2", fr-FR)
-> -1,05E+003
"F" or "f" Fixed-point Result: Integral and decimal digits with optional negative sign.

Supported by: All numeric types.

Precision specifier: Number of decimal digits.

Default precision specifier: Defined by System.Globalization.NumberFormatInfo.NumberDecimalDigits.

More information: The Fixed-Point ("F") Format Specifier.
1234.567 ("F", en-US)
-> 1234.57

1234.567 ("F", de-DE)
-> 1234,57

1234 ("F1", en-US)
-> 1234.0

1234 ("F1", de-DE)
-> 1234,0

-1234.56 ("F4", en-US)
-> -1234.5600

-1234.56 ("F4", de-DE)
-> -1234,5600
"G" or "g" General Result: The more compact of either fixed-point or scientific notation.

Supported by: All numeric types.

Precision specifier: Number of significant digits.

Default precision specifier: Depends on numeric type.

More information: The General ("G") Format Specifier.
-123.456 ("G", en-US)
-> -123.456

-123.456 ("G", sv-SE)
-> -123,456

123.4546 ("G4", en-US)
-> 123.5

123.4546 ("G4", sv-SE)
-> 123,5

-1.234567890e-25 ("G", en-US)
-> -1.23456789E-25

-1.234567890e-25 ("G", sv-SE)
-> -1,23456789E-25
"N" or "n" Number Result: Integral and decimal digits, group separators, and a decimal separator with optional negative sign.

Supported by: All numeric types.

Precision specifier: Desired number of decimal places.

Default precision specifier: Defined by System.Globalization.NumberFormatInfo.NumberDecimalDigits.

More information: The Numeric ("N") Format Specifier.
1234.567 ("N", en-US)
-> 1,234.57

1234.567 ("N", ru-RU)
-> 1 234,57

1234 ("N1", en-US)
-> 1,234.0

1234 ("N1", ru-RU)
-> 1 234,0

-1234.56 ("N3", en-US)
-> -1,234.560

-1234.56 ("N3", ru-RU)
-> -1 234,560
"P" or "p" Percent Result: Number multiplied by 100 and displayed with a percent symbol.

Supported by: All numeric types.

Precision specifier: Desired number of decimal places.

Default precision specifier: Defined by System.Globalization.NumberFormatInfo.PercentDecimalDigits.

More information: The Percent ("P") Format Specifier.
1 ("P", en-US)
-> 100.00 %

1 ("P", fr-FR)
-> 100,00 %

-0.39678 ("P1", en-US)
-> -39.7 %

-0.39678 ("P1", fr-FR)
-> -39,7 %
"R" or "r" Round-trip Result: A string that can round-trip to an identical number.

Supported by: System.Single, System.Double, and System.Numerics.BigInteger.

Note: Recommended for the System.Numerics.BigInteger type only. For System.Double types, use "G17"; for System.Single types, use "G9".
Precision specifier: Ignored.

More information: The Round-trip ("R") Format Specifier.
123456789.12345678 ("R")
-> 123456789.12345678

-1234567890.12345678 ("R")
-> -1234567890.1234567
"X" or "x" Hexadecimal Result: A hexadecimal string.

Supported by: Integral types only.

Precision specifier: Number of digits in the result string.

More information: The Hexadecimal ("X") Format Specifier.
255 ("X")
-> FF

-1 ("x")
-> ff

255 ("x4")
-> 00ff

-1 ("X4")
-> 00FF
Any other single character Unknown specifier Result: Throws a System.FormatException at runtime.

Use standard numeric format strings

A standard numeric format string can be used to define the formatting of a numeric value in one of the following ways:

  • It can be passed to the TryFormat method or an overload of the ToString method that has a format parameter. The following example formats a numeric value as a currency string in the current culture (in this case, the en-US culture).

    sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.Numeric.Standard/cs/standardusage1.cs10

    csharp
    using System;
    
    public class Example
    {
       public static void Main()
       {
          ShowToString();
          ShowComposite();
          ShowCompositeWithAlignment();
       }
    
       private static void ShowToString()
       {
          // <Snippet10>
          decimal value = 123.456m;
          Console.WriteLine(value.ToString("C2"));
          // Displays $123.46
          // </Snippet10>
       }
    
       private static void ShowComposite()
       {
          // <Snippet11>
          decimal value = 123.456m;
          Console.WriteLine($"Your account balance is {value:C2}.");
          // Displays "Your account balance is $123.46."
          // </Snippet11>
       }
    
       private static void ShowCompositeWithAlignment()
       {
          // <Snippet12>
          decimal[] amounts = { 16305.32m, 18794.16m };
          Console.WriteLine("   Beginning Balance           Ending Balance");
          Console.WriteLine("   {0,-28:C2}{1,14:C2}", amounts[0], amounts[1]);
          // Displays:
          //        Beginning Balance           Ending Balance
          //        $16,305.32                      $18,794.16
          // </Snippet12>
       }
    }

    sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.Numeric.Standard/vb/standardusage1.vb10

    vb
    ' Visual Basic .NET Document
    Option Strict On
    
    Module Example
        Public Sub Main()
            ShowToString()
            ShowComposite()
            ShowCompositeWithAlignment()
        End Sub
    
        Private Sub ShowToString()
            ' <Snippet10>
            Dim value As Decimal = 123.456d
            Console.WriteLine(value.ToString("C2"))
            ' Displays $123.46
            ' </Snippet10>
        End Sub
    
        Private Sub ShowComposite()
            ' <Snippet11>   
            Dim value As Decimal = 123.456d
            Console.WriteLine("Your account balance is {0:C2}.", value)
            ' Displays "Your account balance is $123.46."
            ' </Snippet11>
        End Sub
    
        Private Sub ShowCompositeWithAlignment()
            ' <Snippet12>
            Dim amounts() As Decimal = {16305.32d, 18794.16d}
            Console.WriteLine("   Beginning Balance           Ending Balance")
            Console.WriteLine("   {0,-28:C2}{1,14:C2}", amounts(0), amounts(1))
            ' Displays:
            '        Beginning Balance           Ending Balance
            '        $16,305.32                      $18,794.16      
            ' </Snippet12>
        End Sub
    End Module

  • It can be supplied as the formatString argument in a format item used with such methods as System.String.Format, System.Console.WriteLine, and System.Text.StringBuilder.AppendFormat. For more information, see Composite Formatting. The following example uses a format item to insert a currency value in a string.

    sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.Numeric.Standard/cs/standardusage1.cs11

    csharp
    using System;
    
    public class Example
    {
       public static void Main()
       {
          ShowToString();
          ShowComposite();
          ShowCompositeWithAlignment();
       }
    
       private static void ShowToString()
       {
          // <Snippet10>
          decimal value = 123.456m;
          Console.WriteLine(value.ToString("C2"));
          // Displays $123.46
          // </Snippet10>
       }
    
       private static void ShowComposite()
       {
          // <Snippet11>
          decimal value = 123.456m;
          Console.WriteLine($"Your account balance is {value:C2}.");
          // Displays "Your account balance is $123.46."
          // </Snippet11>
       }
    
       private static void ShowCompositeWithAlignment()
       {
          // <Snippet12>
          decimal[] amounts = { 16305.32m, 18794.16m };
          Console.WriteLine("   Beginning Balance           Ending Balance");
          Console.WriteLine("   {0,-28:C2}{1,14:C2}", amounts[0], amounts[1]);
          // Displays:
          //        Beginning Balance           Ending Balance
          //        $16,305.32                      $18,794.16
          // </Snippet12>
       }
    }

    sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.Numeric.Standard/vb/standardusage1.vb11

    vb
    ' Visual Basic .NET Document
    Option Strict On
    
    Module Example
        Public Sub Main()
            ShowToString()
            ShowComposite()
            ShowCompositeWithAlignment()
        End Sub
    
        Private Sub ShowToString()
            ' <Snippet10>
            Dim value As Decimal = 123.456d
            Console.WriteLine(value.ToString("C2"))
            ' Displays $123.46
            ' </Snippet10>
        End Sub
    
        Private Sub ShowComposite()
            ' <Snippet11>   
            Dim value As Decimal = 123.456d
            Console.WriteLine("Your account balance is {0:C2}.", value)
            ' Displays "Your account balance is $123.46."
            ' </Snippet11>
        End Sub
    
        Private Sub ShowCompositeWithAlignment()
            ' <Snippet12>
            Dim amounts() As Decimal = {16305.32d, 18794.16d}
            Console.WriteLine("   Beginning Balance           Ending Balance")
            Console.WriteLine("   {0,-28:C2}{1,14:C2}", amounts(0), amounts(1))
            ' Displays:
            '        Beginning Balance           Ending Balance
            '        $16,305.32                      $18,794.16      
            ' </Snippet12>
        End Sub
    End Module

    Optionally, you can supply an alignment argument to specify the width of the numeric field and whether its value is right- or left-aligned. The following example left-aligns a currency value in a 28-character field, and it right-aligns a currency value in a 14-character field.

    sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.Numeric.Standard/cs/standardusage1.cs12

    csharp
    using System;
    
    public class Example
    {
       public static void Main()
       {
          ShowToString();
          ShowComposite();
          ShowCompositeWithAlignment();
       }
    
       private static void ShowToString()
       {
          // <Snippet10>
          decimal value = 123.456m;
          Console.WriteLine(value.ToString("C2"));
          // Displays $123.46
          // </Snippet10>
       }
    
       private static void ShowComposite()
       {
          // <Snippet11>
          decimal value = 123.456m;
          Console.WriteLine($"Your account balance is {value:C2}.");
          // Displays "Your account balance is $123.46."
          // </Snippet11>
       }
    
       private static void ShowCompositeWithAlignment()
       {
          // <Snippet12>
          decimal[] amounts = { 16305.32m, 18794.16m };
          Console.WriteLine("   Beginning Balance           Ending Balance");
          Console.WriteLine("   {0,-28:C2}{1,14:C2}", amounts[0], amounts[1]);
          // Displays:
          //        Beginning Balance           Ending Balance
          //        $16,305.32                      $18,794.16
          // </Snippet12>
       }
    }

    sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.Numeric.Standard/vb/standardusage1.vb12

    vb
    ' Visual Basic .NET Document
    Option Strict On
    
    Module Example
        Public Sub Main()
            ShowToString()
            ShowComposite()
            ShowCompositeWithAlignment()
        End Sub
    
        Private Sub ShowToString()
            ' <Snippet10>
            Dim value As Decimal = 123.456d
            Console.WriteLine(value.ToString("C2"))
            ' Displays $123.46
            ' </Snippet10>
        End Sub
    
        Private Sub ShowComposite()
            ' <Snippet11>   
            Dim value As Decimal = 123.456d
            Console.WriteLine("Your account balance is {0:C2}.", value)
            ' Displays "Your account balance is $123.46."
            ' </Snippet11>
        End Sub
    
        Private Sub ShowCompositeWithAlignment()
            ' <Snippet12>
            Dim amounts() As Decimal = {16305.32d, 18794.16d}
            Console.WriteLine("   Beginning Balance           Ending Balance")
            Console.WriteLine("   {0,-28:C2}{1,14:C2}", amounts(0), amounts(1))
            ' Displays:
            '        Beginning Balance           Ending Balance
            '        $16,305.32                      $18,794.16      
            ' </Snippet12>
        End Sub
    End Module

  • It can be supplied as the formatString argument in an interpolated expression item of an interpolated string. For more information, see the String interpolation article in the C# reference or the Interpolated strings article in the Visual Basic reference.

The following sections provide detailed information about each of the standard numeric format strings.

Binary format specifier (B)

The binary ("B") format specifier converts a number to a string of binary digits. This format is supported only for integral types and only on .NET 8+.

The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.

For System.Numerics.BigInteger, positive values always have a leading zero to distinguish them from negative values. This ensures the output round-trips to the original value when parsed. For instance, the number 3 converted with the format specifier "B2" is "011". That's because the binary number "11" represents the negative value -1, as it is interpreted as a number with exactly 2 bits due to the "B2" format.

The result string is not affected by the formatting information of the current System.Globalization.NumberFormatInfo object.

Currency format specifier (C)

The "C" (or currency) format specifier converts a number to a string that represents a currency amount. The precision specifier indicates the desired number of decimal places in the result string. If the precision specifier is omitted, the default precision is defined by the System.Globalization.NumberFormatInfo.CurrencyDecimalDigits property.

If the value to be formatted has more than the specified or default number of decimal places, the fractional value is rounded in the result string. If the value to the right of the number of specified decimal places is 5 or greater, the last digit in the result string is rounded away from zero.

The result string is affected by the formatting information of the current System.Globalization.NumberFormatInfo object. The following table lists the System.Globalization.NumberFormatInfo properties that control the formatting of the returned string.

NumberFormatInfo property Description
System.Globalization.NumberFormatInfo.CurrencyPositivePattern Defines the placement of the currency symbol for positive values.
System.Globalization.NumberFormatInfo.CurrencyNegativePattern Defines the placement of the currency symbol for negative values, and specifies whether the negative sign is represented by parentheses or the System.Globalization.NumberFormatInfo.NegativeSign property.
System.Globalization.NumberFormatInfo.NegativeSign Defines the negative sign used if System.Globalization.NumberFormatInfo.CurrencyNegativePattern indicates that parentheses are not used.
System.Globalization.NumberFormatInfo.CurrencySymbol Defines the currency symbol.
System.Globalization.NumberFormatInfo.CurrencyDecimalDigits Defines the default number of decimal digits in a currency value. This value can be overridden by using the precision specifier.
System.Globalization.NumberFormatInfo.CurrencyDecimalSeparator Defines the string that separates integral and decimal digits.
System.Globalization.NumberFormatInfo.CurrencyGroupSeparator Defines the string that separates groups of integral numbers.
System.Globalization.NumberFormatInfo.CurrencyGroupSizes Defines the number of integer digits that appear in a group.

The following example formats a System.Double value with the currency format specifier:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.Numeric.Standard/cs/Standard.cs1

csharp
using System;
using System.Globalization;

class formatting
{
   public static void Main()
   {
      Console.Clear();
      Console.WriteLine(CultureInfo.CurrentUICulture.Name);
      Console.WriteLine(CultureInfo.CurrentCulture.Name);
      Console.WriteLine();
      Console.WriteLine("Currency Format Specifier:");
      ShowCurrency();
      Console.WriteLine();
      Console.WriteLine("Decimal Format Specifier:");
      ShowDecimal();
      Console.WriteLine();
      Console.WriteLine("Exponentiation Format Specifier:");
      ShowExponentiation();
      Console.WriteLine();
      Console.WriteLine("Fixed Point Format Specifier:");
      ShowFixedPoint();
      Console.WriteLine();
      Console.WriteLine("'G' Format Specifier:");
      ShowGeneral();
      Console.WriteLine();
      Console.WriteLine("'N' Format Specifier:");
      ShowNumeric();
      Console.WriteLine();
      Console.WriteLine("Percent Format Specifier:");
      ShowPercent();
      Console.WriteLine();
      Console.WriteLine("Round-trip Format Specifier:");
      ShowRoundTrip();
      Console.WriteLine();
      Console.WriteLine("Hexadecimal Format Specifier:");
      ShowHex();
   }

   public static void ShowCurrency()
   {
      // <Snippet1>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3",
                        CultureInfo.CreateSpecificCulture("da-DK")));
      // The example displays the following output on a system whose
      // current culture is English (United States):
      //       $12,345.68
      //       $12,345.679
      //       12.345,679 kr
      // </Snippet1>
   }

   public static void ShowDecimal()
   {
      // <Snippet2>
      int value;

      value = 12345;
      Console.WriteLine(value.ToString("D"));
      // Displays 12345
      Console.WriteLine(value.ToString("D8"));
      // Displays 00012345

      value = -12345;
      Console.WriteLine(value.ToString("D"));
      // Displays -12345
      Console.WriteLine(value.ToString("D8"));
      // Displays -00012345
      // </Snippet2>
   }

   public static void ShowExponentiation()
   {
      // <Snippet3>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture));
      // Displays 1.234568E+004

      Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture));
      // Displays 1.2345678900E+004

      Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture));
      // Displays 1.2346e+004

      Console.WriteLine(value.ToString("E",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 1,234568E+004
      // </Snippet3>
   }

   public static void ShowFixedPoint()
   {
      // <Snippet4>
      int integerNumber;
      integerNumber = 17843;
      Console.WriteLine(integerNumber.ToString("F",
                        CultureInfo.InvariantCulture));
      // Displays 17843.00

      integerNumber = -29541;
      Console.WriteLine(integerNumber.ToString("F3",
                        CultureInfo.InvariantCulture));
      // Displays -29541.000

      double doubleNumber;
      doubleNumber = 18934.1879;
      Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture));
      // Displays 18934.19

      Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture));
      // Displays 18934

      doubleNumber = -1898300.1987;
      Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture));
      // Displays -1898300.2

      Console.WriteLine(doubleNumber.ToString("F3",
                        CultureInfo.CreateSpecificCulture("es-ES")));
      // Displays -1898300,199
      // </Snippet4>
   }

   public static void ShowGeneral()
   {
      // <Snippet5>
      double number;

      number = 12345.6789;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays  12345.6789
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 12345,6789

      Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture));
      // Displays 12345.68

      number = .0000023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 2.3E-06
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 2,3E-06

      number = .0023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 0.0023

      number = 1234;
      Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture));
      // Displays 1.2E+03

      number = Math.PI;
      Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture));
      // Displays 3.1416
      // </Snippet5>
   }

   public static void ShowNumeric()
   {
      // <Snippet6>
      double dblValue = -12445.6789;
      Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture));
      // Displays -12,445.68
      Console.WriteLine(dblValue.ToString("N1",
                        CultureInfo.CreateSpecificCulture("sv-SE")));
      // Displays -12 445,7

      int intValue = 123456789;
      Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture));
      // Displays 123,456,789.0
      // </Snippet6>
   }

   public static void ShowPercent()
   {
      // <Snippet7>
      double number = .2468013;
      Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture));
      // Displays 24.68 %
      Console.WriteLine(number.ToString("P",
                        CultureInfo.CreateSpecificCulture("hr-HR")));
      // Displays 24,68%
      Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture));
      // Displays 24.7 %
      // </Snippet7>
   }

   public static void ShowRoundTrip()
   {
      // <Snippet8>
      double value;

      value = Math.PI;
      Console.WriteLine(value.ToString("r"));
      // Displays 3.1415926535897931
      Console.WriteLine(value.ToString("r",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 3,1415926535897931
      value = 1.623e-21;
      Console.WriteLine(value.ToString("r"));
      // Displays 1.623E-21
      // </Snippet8>
   }

   public static void ShowHex()
   {
      // <Snippet9>
      int value;

      value = 0x2045e;
      Console.WriteLine(value.ToString("x"));
      // Displays 2045e
      Console.WriteLine(value.ToString("X"));
      // Displays 2045E
      Console.WriteLine(value.ToString("X8"));
      // Displays 0002045E

      value = 123456789;
      Console.WriteLine(value.ToString("X"));
      // Displays 75BCD15
      Console.WriteLine(value.ToString("X2"));
      // Displays 75BCD15
      // </Snippet9>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.Numeric.Standard/vb/Standard.vb1

vb
' Visual Basic .NET Document
Option Strict On

Imports System.Globalization

Module modMain

    Public Sub Main()
        Console.Clear()
        Console.WriteLine(CultureInfo.CurrentUICulture.Name)
        Console.WriteLine(CultureInfo.CurrentCulture.Name)
        Console.WriteLine()
        Console.WriteLine("Currency Format Specifier:")
        ShowCurrency()
        Console.WriteLine()
        Console.WriteLine("Decimal Format Specifier:")
        ShowDecimal()
        Console.WriteLine()
        Console.WriteLine("Exponentiation Format Specifier:")
        ShowExponentiation()
        Console.WriteLine()
        Console.WriteLine("Fixed Point Format Specifier:")
        ShowFixedPoint()
        Console.WriteLine()
        Console.WriteLine("'G' Format Specifier:")
        ShowGeneral()
        Console.WriteLine()
        Console.WriteLine("'N' Format Specifier:")
        ShowNumeric()
        Console.WriteLine()
        Console.WriteLine("Percent Format Specifier:")
        ShowPercent()
        Console.WriteLine()
        Console.WriteLine("Round-trip Format Specifier:")
        ShowRoundTrip()
        Console.WriteLine()
        Console.WriteLine("Hexadecimal Format Specifier:")
        ShowHex()
        Console.WriteLine()
        Console.WriteLine("Invalid Format Specifier")
        ShowInvalid()
    End Sub

    Private Sub ShowCurrency()
        ' <Snippet1>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", _
                          CultureInfo.CreateSpecificCulture("da-DK")))
        ' The example displays the following output on a system whose
        ' current culture is English (United States):
        '       $12,345.68
        '       $12,345.679
        '       kr 12.345,679
        ' </Snippet1> 
    End Sub

    Private Sub ShowDecimal()
        ' <Snippet2>
        Dim value As Integer

        value = 12345
        Console.WriteLine(value.ToString("D"))
        ' Displays 12345   
        Console.WriteLine(value.ToString("D8"))
        ' Displays 00012345

        value = -12345
        Console.WriteLine(value.ToString("D"))
        ' Displays -12345
        Console.WriteLine(value.ToString("D8"))
        ' Displays -00012345
        ' </Snippet2>
    End Sub

    Private Sub ShowExponentiation()
        ' <Snippet3>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture))
        ' Displays 1.234568E+004

        Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture))
        ' Displays 1.2345678900E+004

        Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture))
        ' Displays 1.2346e+004

        Console.WriteLine(value.ToString("E", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 1,234568E+004
        ' </Snippet3>
    End Sub

    Private Sub ShowFixedPoint()
        ' <Snippet4>
        Dim integerNumber As Integer
        integerNumber = 17843
        Console.WriteLine(integerNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 17843.00

        integerNumber = -29541
        Console.WriteLine(integerNumber.ToString("F3", CultureInfo.InvariantCulture))
        ' Displays -29541.000

        Dim doubleNumber As Double
        doubleNumber = 18934.1879
        Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 18934.19

        Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture))
        ' Displays 18934

        doubleNumber = -1898300.1987
        Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture))
        ' Displays -1898300.2

        Console.WriteLine(doubleNumber.ToString("F3", _
                          CultureInfo.CreateSpecificCulture("es-ES")))
        ' Displays -1898300,199                        
        ' </Snippet4>
    End Sub

    Private Sub ShowGeneral()
        ' <Snippet5>
        Dim number As Double

        number = 12345.6789
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays  12345.6789
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 12345,6789

        Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture))
        ' Displays 12345.68 

        number = .0000023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 2.3E-06       
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 2,3E-06

        number = .0023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 0.0023

        number = 1234
        Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture))
        ' Displays 1.2E+03

        number = Math.Pi
        Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture))
        ' Displays 3.1416    
        ' </Snippet5>
    End Sub

    Private Sub ShowNumeric()
        ' <Snippet6>
        Dim dblValue As Double = -12445.6789
        Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
        ' Displays -12,445.68
        Console.WriteLine(dblValue.ToString("N1", _
                          CultureInfo.CreateSpecificCulture("sv-SE")))
        ' Displays -12 445,7

        Dim intValue As Integer = 123456789
        Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture))
        ' Displays 123,456,789.0 
        ' </Snippet6>
    End Sub

    Private Sub ShowPercent()
        ' <Snippet7>
        Dim number As Double = .2468013
        Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture))
        ' Displays 24.68 %
        Console.WriteLine(number.ToString("P", _
                          CultureInfo.CreateSpecificCulture("hr-HR")))
        ' Displays 24,68%     
        Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture))
        ' Displays 24.7 %
        ' </Snippet7>
    End Sub

    Private Sub ShowRoundTrip()
        ' <Snippet8>
        Dim value As Double

        value = Math.Pi
        Console.WriteLine(value.ToString("r"))
        ' Displays 3.1415926535897931
        Console.WriteLine(value.ToString("r", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 3,1415926535897931
        value = 1.623e-21
        Console.WriteLine(value.ToString("r"))
        ' Displays 1.623E-21
        ' </Snippet8>
    End Sub

    Private Sub ShowHex()
        ' <Snippet9>
        Dim value As Integer

        value = &h2045e
        Console.WriteLine(value.ToString("x"))
        ' Displays 2045e
        Console.WriteLine(value.ToString("X"))
        ' Displays 2045E
        Console.WriteLine(value.ToString("X8"))
        ' Displays 0002045E

        value = 123456789
        Console.WriteLine(value.ToString("X"))
        ' Displays 75BCD15
        Console.WriteLine(value.ToString("X2"))
        ' Displays 75BCD15
        ' </Snippet9>
    End Sub

    Private Sub ShowInvalid()
        Dim value As Integer = 12
        Dim specifier AS String = "Z"
        Try
            Console.WriteLine(value.ToString("Z"))
        Catch e As Exception
            Console.WriteLine("{0}: {1} is not a valid format specifier.", _
                              e.GetType().Name, specifier)
        End Try
    End Sub
End Module

Decimal format specifier (D)

The "D" (or decimal) format specifier converts a number to a string of decimal digits (0-9), prefixed by a minus sign if the number is negative. This format is supported only for integral types.

The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. If no precision specifier is specified, the default is the minimum value required to represent the integer without leading zeros.

The result string is affected by the formatting information of the current System.Globalization.NumberFormatInfo object. As the following table shows, a single property affects the formatting of the result string.

NumberFormatInfo property Description
System.Globalization.NumberFormatInfo.NegativeSign Defines the string that indicates that a number is negative.

The following example formats an System.Int32 value with the decimal format specifier.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.Numeric.Standard/cs/Standard.cs2

csharp
using System;
using System.Globalization;

class formatting
{
   public static void Main()
   {
      Console.Clear();
      Console.WriteLine(CultureInfo.CurrentUICulture.Name);
      Console.WriteLine(CultureInfo.CurrentCulture.Name);
      Console.WriteLine();
      Console.WriteLine("Currency Format Specifier:");
      ShowCurrency();
      Console.WriteLine();
      Console.WriteLine("Decimal Format Specifier:");
      ShowDecimal();
      Console.WriteLine();
      Console.WriteLine("Exponentiation Format Specifier:");
      ShowExponentiation();
      Console.WriteLine();
      Console.WriteLine("Fixed Point Format Specifier:");
      ShowFixedPoint();
      Console.WriteLine();
      Console.WriteLine("'G' Format Specifier:");
      ShowGeneral();
      Console.WriteLine();
      Console.WriteLine("'N' Format Specifier:");
      ShowNumeric();
      Console.WriteLine();
      Console.WriteLine("Percent Format Specifier:");
      ShowPercent();
      Console.WriteLine();
      Console.WriteLine("Round-trip Format Specifier:");
      ShowRoundTrip();
      Console.WriteLine();
      Console.WriteLine("Hexadecimal Format Specifier:");
      ShowHex();
   }

   public static void ShowCurrency()
   {
      // <Snippet1>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3",
                        CultureInfo.CreateSpecificCulture("da-DK")));
      // The example displays the following output on a system whose
      // current culture is English (United States):
      //       $12,345.68
      //       $12,345.679
      //       12.345,679 kr
      // </Snippet1>
   }

   public static void ShowDecimal()
   {
      // <Snippet2>
      int value;

      value = 12345;
      Console.WriteLine(value.ToString("D"));
      // Displays 12345
      Console.WriteLine(value.ToString("D8"));
      // Displays 00012345

      value = -12345;
      Console.WriteLine(value.ToString("D"));
      // Displays -12345
      Console.WriteLine(value.ToString("D8"));
      // Displays -00012345
      // </Snippet2>
   }

   public static void ShowExponentiation()
   {
      // <Snippet3>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture));
      // Displays 1.234568E+004

      Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture));
      // Displays 1.2345678900E+004

      Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture));
      // Displays 1.2346e+004

      Console.WriteLine(value.ToString("E",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 1,234568E+004
      // </Snippet3>
   }

   public static void ShowFixedPoint()
   {
      // <Snippet4>
      int integerNumber;
      integerNumber = 17843;
      Console.WriteLine(integerNumber.ToString("F",
                        CultureInfo.InvariantCulture));
      // Displays 17843.00

      integerNumber = -29541;
      Console.WriteLine(integerNumber.ToString("F3",
                        CultureInfo.InvariantCulture));
      // Displays -29541.000

      double doubleNumber;
      doubleNumber = 18934.1879;
      Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture));
      // Displays 18934.19

      Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture));
      // Displays 18934

      doubleNumber = -1898300.1987;
      Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture));
      // Displays -1898300.2

      Console.WriteLine(doubleNumber.ToString("F3",
                        CultureInfo.CreateSpecificCulture("es-ES")));
      // Displays -1898300,199
      // </Snippet4>
   }

   public static void ShowGeneral()
   {
      // <Snippet5>
      double number;

      number = 12345.6789;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays  12345.6789
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 12345,6789

      Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture));
      // Displays 12345.68

      number = .0000023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 2.3E-06
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 2,3E-06

      number = .0023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 0.0023

      number = 1234;
      Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture));
      // Displays 1.2E+03

      number = Math.PI;
      Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture));
      // Displays 3.1416
      // </Snippet5>
   }

   public static void ShowNumeric()
   {
      // <Snippet6>
      double dblValue = -12445.6789;
      Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture));
      // Displays -12,445.68
      Console.WriteLine(dblValue.ToString("N1",
                        CultureInfo.CreateSpecificCulture("sv-SE")));
      // Displays -12 445,7

      int intValue = 123456789;
      Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture));
      // Displays 123,456,789.0
      // </Snippet6>
   }

   public static void ShowPercent()
   {
      // <Snippet7>
      double number = .2468013;
      Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture));
      // Displays 24.68 %
      Console.WriteLine(number.ToString("P",
                        CultureInfo.CreateSpecificCulture("hr-HR")));
      // Displays 24,68%
      Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture));
      // Displays 24.7 %
      // </Snippet7>
   }

   public static void ShowRoundTrip()
   {
      // <Snippet8>
      double value;

      value = Math.PI;
      Console.WriteLine(value.ToString("r"));
      // Displays 3.1415926535897931
      Console.WriteLine(value.ToString("r",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 3,1415926535897931
      value = 1.623e-21;
      Console.WriteLine(value.ToString("r"));
      // Displays 1.623E-21
      // </Snippet8>
   }

   public static void ShowHex()
   {
      // <Snippet9>
      int value;

      value = 0x2045e;
      Console.WriteLine(value.ToString("x"));
      // Displays 2045e
      Console.WriteLine(value.ToString("X"));
      // Displays 2045E
      Console.WriteLine(value.ToString("X8"));
      // Displays 0002045E

      value = 123456789;
      Console.WriteLine(value.ToString("X"));
      // Displays 75BCD15
      Console.WriteLine(value.ToString("X2"));
      // Displays 75BCD15
      // </Snippet9>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.Numeric.Standard/vb/Standard.vb2

vb
' Visual Basic .NET Document
Option Strict On

Imports System.Globalization

Module modMain

    Public Sub Main()
        Console.Clear()
        Console.WriteLine(CultureInfo.CurrentUICulture.Name)
        Console.WriteLine(CultureInfo.CurrentCulture.Name)
        Console.WriteLine()
        Console.WriteLine("Currency Format Specifier:")
        ShowCurrency()
        Console.WriteLine()
        Console.WriteLine("Decimal Format Specifier:")
        ShowDecimal()
        Console.WriteLine()
        Console.WriteLine("Exponentiation Format Specifier:")
        ShowExponentiation()
        Console.WriteLine()
        Console.WriteLine("Fixed Point Format Specifier:")
        ShowFixedPoint()
        Console.WriteLine()
        Console.WriteLine("'G' Format Specifier:")
        ShowGeneral()
        Console.WriteLine()
        Console.WriteLine("'N' Format Specifier:")
        ShowNumeric()
        Console.WriteLine()
        Console.WriteLine("Percent Format Specifier:")
        ShowPercent()
        Console.WriteLine()
        Console.WriteLine("Round-trip Format Specifier:")
        ShowRoundTrip()
        Console.WriteLine()
        Console.WriteLine("Hexadecimal Format Specifier:")
        ShowHex()
        Console.WriteLine()
        Console.WriteLine("Invalid Format Specifier")
        ShowInvalid()
    End Sub

    Private Sub ShowCurrency()
        ' <Snippet1>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", _
                          CultureInfo.CreateSpecificCulture("da-DK")))
        ' The example displays the following output on a system whose
        ' current culture is English (United States):
        '       $12,345.68
        '       $12,345.679
        '       kr 12.345,679
        ' </Snippet1> 
    End Sub

    Private Sub ShowDecimal()
        ' <Snippet2>
        Dim value As Integer

        value = 12345
        Console.WriteLine(value.ToString("D"))
        ' Displays 12345   
        Console.WriteLine(value.ToString("D8"))
        ' Displays 00012345

        value = -12345
        Console.WriteLine(value.ToString("D"))
        ' Displays -12345
        Console.WriteLine(value.ToString("D8"))
        ' Displays -00012345
        ' </Snippet2>
    End Sub

    Private Sub ShowExponentiation()
        ' <Snippet3>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture))
        ' Displays 1.234568E+004

        Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture))
        ' Displays 1.2345678900E+004

        Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture))
        ' Displays 1.2346e+004

        Console.WriteLine(value.ToString("E", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 1,234568E+004
        ' </Snippet3>
    End Sub

    Private Sub ShowFixedPoint()
        ' <Snippet4>
        Dim integerNumber As Integer
        integerNumber = 17843
        Console.WriteLine(integerNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 17843.00

        integerNumber = -29541
        Console.WriteLine(integerNumber.ToString("F3", CultureInfo.InvariantCulture))
        ' Displays -29541.000

        Dim doubleNumber As Double
        doubleNumber = 18934.1879
        Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 18934.19

        Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture))
        ' Displays 18934

        doubleNumber = -1898300.1987
        Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture))
        ' Displays -1898300.2

        Console.WriteLine(doubleNumber.ToString("F3", _
                          CultureInfo.CreateSpecificCulture("es-ES")))
        ' Displays -1898300,199                        
        ' </Snippet4>
    End Sub

    Private Sub ShowGeneral()
        ' <Snippet5>
        Dim number As Double

        number = 12345.6789
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays  12345.6789
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 12345,6789

        Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture))
        ' Displays 12345.68 

        number = .0000023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 2.3E-06       
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 2,3E-06

        number = .0023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 0.0023

        number = 1234
        Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture))
        ' Displays 1.2E+03

        number = Math.Pi
        Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture))
        ' Displays 3.1416    
        ' </Snippet5>
    End Sub

    Private Sub ShowNumeric()
        ' <Snippet6>
        Dim dblValue As Double = -12445.6789
        Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
        ' Displays -12,445.68
        Console.WriteLine(dblValue.ToString("N1", _
                          CultureInfo.CreateSpecificCulture("sv-SE")))
        ' Displays -12 445,7

        Dim intValue As Integer = 123456789
        Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture))
        ' Displays 123,456,789.0 
        ' </Snippet6>
    End Sub

    Private Sub ShowPercent()
        ' <Snippet7>
        Dim number As Double = .2468013
        Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture))
        ' Displays 24.68 %
        Console.WriteLine(number.ToString("P", _
                          CultureInfo.CreateSpecificCulture("hr-HR")))
        ' Displays 24,68%     
        Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture))
        ' Displays 24.7 %
        ' </Snippet7>
    End Sub

    Private Sub ShowRoundTrip()
        ' <Snippet8>
        Dim value As Double

        value = Math.Pi
        Console.WriteLine(value.ToString("r"))
        ' Displays 3.1415926535897931
        Console.WriteLine(value.ToString("r", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 3,1415926535897931
        value = 1.623e-21
        Console.WriteLine(value.ToString("r"))
        ' Displays 1.623E-21
        ' </Snippet8>
    End Sub

    Private Sub ShowHex()
        ' <Snippet9>
        Dim value As Integer

        value = &h2045e
        Console.WriteLine(value.ToString("x"))
        ' Displays 2045e
        Console.WriteLine(value.ToString("X"))
        ' Displays 2045E
        Console.WriteLine(value.ToString("X8"))
        ' Displays 0002045E

        value = 123456789
        Console.WriteLine(value.ToString("X"))
        ' Displays 75BCD15
        Console.WriteLine(value.ToString("X2"))
        ' Displays 75BCD15
        ' </Snippet9>
    End Sub

    Private Sub ShowInvalid()
        Dim value As Integer = 12
        Dim specifier AS String = "Z"
        Try
            Console.WriteLine(value.ToString("Z"))
        Catch e As Exception
            Console.WriteLine("{0}: {1} is not a valid format specifier.", _
                              e.GetType().Name, specifier)
        End Try
    End Sub
End Module

Exponential format specifier (E)

The exponential ("E") format specifier converts a number to a string of the form "-d.ddd…E+ddd" or "-d.ddd…e+ddd", where each "d" indicates a digit (0-9). The string starts with a minus sign if the number is negative. Exactly one digit always precedes the decimal point.

The precision specifier indicates the desired number of digits after the decimal point. If the precision specifier is omitted, a default of six digits after the decimal point is used.

The case of the format specifier indicates whether to prefix the exponent with an "E" or an "e". The exponent always consists of a plus or minus sign and a minimum of three digits. The exponent is padded with zeros to meet this minimum, if required.

The result string is affected by the formatting information of the current System.Globalization.NumberFormatInfo object. The following table lists the System.Globalization.NumberFormatInfo properties that control the formatting of the returned string.

NumberFormatInfo property Description
System.Globalization.NumberFormatInfo.NegativeSign Defines the string that indicates that a number is negative for both the coefficient and exponent.
System.Globalization.NumberFormatInfo.NumberDecimalSeparator Defines the string that separates the integral digit from decimal digits in the coefficient.
System.Globalization.NumberFormatInfo.PositiveSign Defines the string that indicates that an exponent is positive.

The following example formats a System.Double value with the exponential format specifier:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.Numeric.Standard/cs/Standard.cs3

csharp
using System;
using System.Globalization;

class formatting
{
   public static void Main()
   {
      Console.Clear();
      Console.WriteLine(CultureInfo.CurrentUICulture.Name);
      Console.WriteLine(CultureInfo.CurrentCulture.Name);
      Console.WriteLine();
      Console.WriteLine("Currency Format Specifier:");
      ShowCurrency();
      Console.WriteLine();
      Console.WriteLine("Decimal Format Specifier:");
      ShowDecimal();
      Console.WriteLine();
      Console.WriteLine("Exponentiation Format Specifier:");
      ShowExponentiation();
      Console.WriteLine();
      Console.WriteLine("Fixed Point Format Specifier:");
      ShowFixedPoint();
      Console.WriteLine();
      Console.WriteLine("'G' Format Specifier:");
      ShowGeneral();
      Console.WriteLine();
      Console.WriteLine("'N' Format Specifier:");
      ShowNumeric();
      Console.WriteLine();
      Console.WriteLine("Percent Format Specifier:");
      ShowPercent();
      Console.WriteLine();
      Console.WriteLine("Round-trip Format Specifier:");
      ShowRoundTrip();
      Console.WriteLine();
      Console.WriteLine("Hexadecimal Format Specifier:");
      ShowHex();
   }

   public static void ShowCurrency()
   {
      // <Snippet1>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3",
                        CultureInfo.CreateSpecificCulture("da-DK")));
      // The example displays the following output on a system whose
      // current culture is English (United States):
      //       $12,345.68
      //       $12,345.679
      //       12.345,679 kr
      // </Snippet1>
   }

   public static void ShowDecimal()
   {
      // <Snippet2>
      int value;

      value = 12345;
      Console.WriteLine(value.ToString("D"));
      // Displays 12345
      Console.WriteLine(value.ToString("D8"));
      // Displays 00012345

      value = -12345;
      Console.WriteLine(value.ToString("D"));
      // Displays -12345
      Console.WriteLine(value.ToString("D8"));
      // Displays -00012345
      // </Snippet2>
   }

   public static void ShowExponentiation()
   {
      // <Snippet3>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture));
      // Displays 1.234568E+004

      Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture));
      // Displays 1.2345678900E+004

      Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture));
      // Displays 1.2346e+004

      Console.WriteLine(value.ToString("E",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 1,234568E+004
      // </Snippet3>
   }

   public static void ShowFixedPoint()
   {
      // <Snippet4>
      int integerNumber;
      integerNumber = 17843;
      Console.WriteLine(integerNumber.ToString("F",
                        CultureInfo.InvariantCulture));
      // Displays 17843.00

      integerNumber = -29541;
      Console.WriteLine(integerNumber.ToString("F3",
                        CultureInfo.InvariantCulture));
      // Displays -29541.000

      double doubleNumber;
      doubleNumber = 18934.1879;
      Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture));
      // Displays 18934.19

      Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture));
      // Displays 18934

      doubleNumber = -1898300.1987;
      Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture));
      // Displays -1898300.2

      Console.WriteLine(doubleNumber.ToString("F3",
                        CultureInfo.CreateSpecificCulture("es-ES")));
      // Displays -1898300,199
      // </Snippet4>
   }

   public static void ShowGeneral()
   {
      // <Snippet5>
      double number;

      number = 12345.6789;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays  12345.6789
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 12345,6789

      Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture));
      // Displays 12345.68

      number = .0000023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 2.3E-06
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 2,3E-06

      number = .0023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 0.0023

      number = 1234;
      Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture));
      // Displays 1.2E+03

      number = Math.PI;
      Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture));
      // Displays 3.1416
      // </Snippet5>
   }

   public static void ShowNumeric()
   {
      // <Snippet6>
      double dblValue = -12445.6789;
      Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture));
      // Displays -12,445.68
      Console.WriteLine(dblValue.ToString("N1",
                        CultureInfo.CreateSpecificCulture("sv-SE")));
      // Displays -12 445,7

      int intValue = 123456789;
      Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture));
      // Displays 123,456,789.0
      // </Snippet6>
   }

   public static void ShowPercent()
   {
      // <Snippet7>
      double number = .2468013;
      Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture));
      // Displays 24.68 %
      Console.WriteLine(number.ToString("P",
                        CultureInfo.CreateSpecificCulture("hr-HR")));
      // Displays 24,68%
      Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture));
      // Displays 24.7 %
      // </Snippet7>
   }

   public static void ShowRoundTrip()
   {
      // <Snippet8>
      double value;

      value = Math.PI;
      Console.WriteLine(value.ToString("r"));
      // Displays 3.1415926535897931
      Console.WriteLine(value.ToString("r",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 3,1415926535897931
      value = 1.623e-21;
      Console.WriteLine(value.ToString("r"));
      // Displays 1.623E-21
      // </Snippet8>
   }

   public static void ShowHex()
   {
      // <Snippet9>
      int value;

      value = 0x2045e;
      Console.WriteLine(value.ToString("x"));
      // Displays 2045e
      Console.WriteLine(value.ToString("X"));
      // Displays 2045E
      Console.WriteLine(value.ToString("X8"));
      // Displays 0002045E

      value = 123456789;
      Console.WriteLine(value.ToString("X"));
      // Displays 75BCD15
      Console.WriteLine(value.ToString("X2"));
      // Displays 75BCD15
      // </Snippet9>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.Numeric.Standard/vb/Standard.vb3

vb
' Visual Basic .NET Document
Option Strict On

Imports System.Globalization

Module modMain

    Public Sub Main()
        Console.Clear()
        Console.WriteLine(CultureInfo.CurrentUICulture.Name)
        Console.WriteLine(CultureInfo.CurrentCulture.Name)
        Console.WriteLine()
        Console.WriteLine("Currency Format Specifier:")
        ShowCurrency()
        Console.WriteLine()
        Console.WriteLine("Decimal Format Specifier:")
        ShowDecimal()
        Console.WriteLine()
        Console.WriteLine("Exponentiation Format Specifier:")
        ShowExponentiation()
        Console.WriteLine()
        Console.WriteLine("Fixed Point Format Specifier:")
        ShowFixedPoint()
        Console.WriteLine()
        Console.WriteLine("'G' Format Specifier:")
        ShowGeneral()
        Console.WriteLine()
        Console.WriteLine("'N' Format Specifier:")
        ShowNumeric()
        Console.WriteLine()
        Console.WriteLine("Percent Format Specifier:")
        ShowPercent()
        Console.WriteLine()
        Console.WriteLine("Round-trip Format Specifier:")
        ShowRoundTrip()
        Console.WriteLine()
        Console.WriteLine("Hexadecimal Format Specifier:")
        ShowHex()
        Console.WriteLine()
        Console.WriteLine("Invalid Format Specifier")
        ShowInvalid()
    End Sub

    Private Sub ShowCurrency()
        ' <Snippet1>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", _
                          CultureInfo.CreateSpecificCulture("da-DK")))
        ' The example displays the following output on a system whose
        ' current culture is English (United States):
        '       $12,345.68
        '       $12,345.679
        '       kr 12.345,679
        ' </Snippet1> 
    End Sub

    Private Sub ShowDecimal()
        ' <Snippet2>
        Dim value As Integer

        value = 12345
        Console.WriteLine(value.ToString("D"))
        ' Displays 12345   
        Console.WriteLine(value.ToString("D8"))
        ' Displays 00012345

        value = -12345
        Console.WriteLine(value.ToString("D"))
        ' Displays -12345
        Console.WriteLine(value.ToString("D8"))
        ' Displays -00012345
        ' </Snippet2>
    End Sub

    Private Sub ShowExponentiation()
        ' <Snippet3>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture))
        ' Displays 1.234568E+004

        Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture))
        ' Displays 1.2345678900E+004

        Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture))
        ' Displays 1.2346e+004

        Console.WriteLine(value.ToString("E", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 1,234568E+004
        ' </Snippet3>
    End Sub

    Private Sub ShowFixedPoint()
        ' <Snippet4>
        Dim integerNumber As Integer
        integerNumber = 17843
        Console.WriteLine(integerNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 17843.00

        integerNumber = -29541
        Console.WriteLine(integerNumber.ToString("F3", CultureInfo.InvariantCulture))
        ' Displays -29541.000

        Dim doubleNumber As Double
        doubleNumber = 18934.1879
        Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 18934.19

        Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture))
        ' Displays 18934

        doubleNumber = -1898300.1987
        Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture))
        ' Displays -1898300.2

        Console.WriteLine(doubleNumber.ToString("F3", _
                          CultureInfo.CreateSpecificCulture("es-ES")))
        ' Displays -1898300,199                        
        ' </Snippet4>
    End Sub

    Private Sub ShowGeneral()
        ' <Snippet5>
        Dim number As Double

        number = 12345.6789
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays  12345.6789
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 12345,6789

        Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture))
        ' Displays 12345.68 

        number = .0000023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 2.3E-06       
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 2,3E-06

        number = .0023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 0.0023

        number = 1234
        Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture))
        ' Displays 1.2E+03

        number = Math.Pi
        Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture))
        ' Displays 3.1416    
        ' </Snippet5>
    End Sub

    Private Sub ShowNumeric()
        ' <Snippet6>
        Dim dblValue As Double = -12445.6789
        Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
        ' Displays -12,445.68
        Console.WriteLine(dblValue.ToString("N1", _
                          CultureInfo.CreateSpecificCulture("sv-SE")))
        ' Displays -12 445,7

        Dim intValue As Integer = 123456789
        Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture))
        ' Displays 123,456,789.0 
        ' </Snippet6>
    End Sub

    Private Sub ShowPercent()
        ' <Snippet7>
        Dim number As Double = .2468013
        Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture))
        ' Displays 24.68 %
        Console.WriteLine(number.ToString("P", _
                          CultureInfo.CreateSpecificCulture("hr-HR")))
        ' Displays 24,68%     
        Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture))
        ' Displays 24.7 %
        ' </Snippet7>
    End Sub

    Private Sub ShowRoundTrip()
        ' <Snippet8>
        Dim value As Double

        value = Math.Pi
        Console.WriteLine(value.ToString("r"))
        ' Displays 3.1415926535897931
        Console.WriteLine(value.ToString("r", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 3,1415926535897931
        value = 1.623e-21
        Console.WriteLine(value.ToString("r"))
        ' Displays 1.623E-21
        ' </Snippet8>
    End Sub

    Private Sub ShowHex()
        ' <Snippet9>
        Dim value As Integer

        value = &h2045e
        Console.WriteLine(value.ToString("x"))
        ' Displays 2045e
        Console.WriteLine(value.ToString("X"))
        ' Displays 2045E
        Console.WriteLine(value.ToString("X8"))
        ' Displays 0002045E

        value = 123456789
        Console.WriteLine(value.ToString("X"))
        ' Displays 75BCD15
        Console.WriteLine(value.ToString("X2"))
        ' Displays 75BCD15
        ' </Snippet9>
    End Sub

    Private Sub ShowInvalid()
        Dim value As Integer = 12
        Dim specifier AS String = "Z"
        Try
            Console.WriteLine(value.ToString("Z"))
        Catch e As Exception
            Console.WriteLine("{0}: {1} is not a valid format specifier.", _
                              e.GetType().Name, specifier)
        End Try
    End Sub
End Module

Fixed-point format specifier (F)

The fixed-point ("F") format specifier converts a number to a string of the form "-ddd.ddd…" where each "d" indicates a digit (0-9). The string starts with a minus sign if the number is negative.

The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the current System.Globalization.NumberFormatInfo.NumberDecimalDigits property supplies the numeric precision.

The result string is affected by the formatting information of the current System.Globalization.NumberFormatInfo object. The following table lists the properties of the System.Globalization.NumberFormatInfo object that control the formatting of the result string.

NumberFormatInfo property Description
System.Globalization.NumberFormatInfo.NegativeSign Defines the string that indicates that a number is negative.
System.Globalization.NumberFormatInfo.NumberDecimalSeparator Defines the string that separates integral digits from decimal digits.
System.Globalization.NumberFormatInfo.NumberDecimalDigits Defines the default number of decimal digits. This value can be overridden by using the precision specifier.

The following example formats a System.Double and an System.Int32 value with the fixed-point format specifier:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.Numeric.Standard/cs/Standard.cs4

csharp
using System;
using System.Globalization;

class formatting
{
   public static void Main()
   {
      Console.Clear();
      Console.WriteLine(CultureInfo.CurrentUICulture.Name);
      Console.WriteLine(CultureInfo.CurrentCulture.Name);
      Console.WriteLine();
      Console.WriteLine("Currency Format Specifier:");
      ShowCurrency();
      Console.WriteLine();
      Console.WriteLine("Decimal Format Specifier:");
      ShowDecimal();
      Console.WriteLine();
      Console.WriteLine("Exponentiation Format Specifier:");
      ShowExponentiation();
      Console.WriteLine();
      Console.WriteLine("Fixed Point Format Specifier:");
      ShowFixedPoint();
      Console.WriteLine();
      Console.WriteLine("'G' Format Specifier:");
      ShowGeneral();
      Console.WriteLine();
      Console.WriteLine("'N' Format Specifier:");
      ShowNumeric();
      Console.WriteLine();
      Console.WriteLine("Percent Format Specifier:");
      ShowPercent();
      Console.WriteLine();
      Console.WriteLine("Round-trip Format Specifier:");
      ShowRoundTrip();
      Console.WriteLine();
      Console.WriteLine("Hexadecimal Format Specifier:");
      ShowHex();
   }

   public static void ShowCurrency()
   {
      // <Snippet1>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3",
                        CultureInfo.CreateSpecificCulture("da-DK")));
      // The example displays the following output on a system whose
      // current culture is English (United States):
      //       $12,345.68
      //       $12,345.679
      //       12.345,679 kr
      // </Snippet1>
   }

   public static void ShowDecimal()
   {
      // <Snippet2>
      int value;

      value = 12345;
      Console.WriteLine(value.ToString("D"));
      // Displays 12345
      Console.WriteLine(value.ToString("D8"));
      // Displays 00012345

      value = -12345;
      Console.WriteLine(value.ToString("D"));
      // Displays -12345
      Console.WriteLine(value.ToString("D8"));
      // Displays -00012345
      // </Snippet2>
   }

   public static void ShowExponentiation()
   {
      // <Snippet3>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture));
      // Displays 1.234568E+004

      Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture));
      // Displays 1.2345678900E+004

      Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture));
      // Displays 1.2346e+004

      Console.WriteLine(value.ToString("E",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 1,234568E+004
      // </Snippet3>
   }

   public static void ShowFixedPoint()
   {
      // <Snippet4>
      int integerNumber;
      integerNumber = 17843;
      Console.WriteLine(integerNumber.ToString("F",
                        CultureInfo.InvariantCulture));
      // Displays 17843.00

      integerNumber = -29541;
      Console.WriteLine(integerNumber.ToString("F3",
                        CultureInfo.InvariantCulture));
      // Displays -29541.000

      double doubleNumber;
      doubleNumber = 18934.1879;
      Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture));
      // Displays 18934.19

      Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture));
      // Displays 18934

      doubleNumber = -1898300.1987;
      Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture));
      // Displays -1898300.2

      Console.WriteLine(doubleNumber.ToString("F3",
                        CultureInfo.CreateSpecificCulture("es-ES")));
      // Displays -1898300,199
      // </Snippet4>
   }

   public static void ShowGeneral()
   {
      // <Snippet5>
      double number;

      number = 12345.6789;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays  12345.6789
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 12345,6789

      Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture));
      // Displays 12345.68

      number = .0000023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 2.3E-06
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 2,3E-06

      number = .0023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 0.0023

      number = 1234;
      Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture));
      // Displays 1.2E+03

      number = Math.PI;
      Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture));
      // Displays 3.1416
      // </Snippet5>
   }

   public static void ShowNumeric()
   {
      // <Snippet6>
      double dblValue = -12445.6789;
      Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture));
      // Displays -12,445.68
      Console.WriteLine(dblValue.ToString("N1",
                        CultureInfo.CreateSpecificCulture("sv-SE")));
      // Displays -12 445,7

      int intValue = 123456789;
      Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture));
      // Displays 123,456,789.0
      // </Snippet6>
   }

   public static void ShowPercent()
   {
      // <Snippet7>
      double number = .2468013;
      Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture));
      // Displays 24.68 %
      Console.WriteLine(number.ToString("P",
                        CultureInfo.CreateSpecificCulture("hr-HR")));
      // Displays 24,68%
      Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture));
      // Displays 24.7 %
      // </Snippet7>
   }

   public static void ShowRoundTrip()
   {
      // <Snippet8>
      double value;

      value = Math.PI;
      Console.WriteLine(value.ToString("r"));
      // Displays 3.1415926535897931
      Console.WriteLine(value.ToString("r",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 3,1415926535897931
      value = 1.623e-21;
      Console.WriteLine(value.ToString("r"));
      // Displays 1.623E-21
      // </Snippet8>
   }

   public static void ShowHex()
   {
      // <Snippet9>
      int value;

      value = 0x2045e;
      Console.WriteLine(value.ToString("x"));
      // Displays 2045e
      Console.WriteLine(value.ToString("X"));
      // Displays 2045E
      Console.WriteLine(value.ToString("X8"));
      // Displays 0002045E

      value = 123456789;
      Console.WriteLine(value.ToString("X"));
      // Displays 75BCD15
      Console.WriteLine(value.ToString("X2"));
      // Displays 75BCD15
      // </Snippet9>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.Numeric.Standard/vb/Standard.vb4

vb
' Visual Basic .NET Document
Option Strict On

Imports System.Globalization

Module modMain

    Public Sub Main()
        Console.Clear()
        Console.WriteLine(CultureInfo.CurrentUICulture.Name)
        Console.WriteLine(CultureInfo.CurrentCulture.Name)
        Console.WriteLine()
        Console.WriteLine("Currency Format Specifier:")
        ShowCurrency()
        Console.WriteLine()
        Console.WriteLine("Decimal Format Specifier:")
        ShowDecimal()
        Console.WriteLine()
        Console.WriteLine("Exponentiation Format Specifier:")
        ShowExponentiation()
        Console.WriteLine()
        Console.WriteLine("Fixed Point Format Specifier:")
        ShowFixedPoint()
        Console.WriteLine()
        Console.WriteLine("'G' Format Specifier:")
        ShowGeneral()
        Console.WriteLine()
        Console.WriteLine("'N' Format Specifier:")
        ShowNumeric()
        Console.WriteLine()
        Console.WriteLine("Percent Format Specifier:")
        ShowPercent()
        Console.WriteLine()
        Console.WriteLine("Round-trip Format Specifier:")
        ShowRoundTrip()
        Console.WriteLine()
        Console.WriteLine("Hexadecimal Format Specifier:")
        ShowHex()
        Console.WriteLine()
        Console.WriteLine("Invalid Format Specifier")
        ShowInvalid()
    End Sub

    Private Sub ShowCurrency()
        ' <Snippet1>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", _
                          CultureInfo.CreateSpecificCulture("da-DK")))
        ' The example displays the following output on a system whose
        ' current culture is English (United States):
        '       $12,345.68
        '       $12,345.679
        '       kr 12.345,679
        ' </Snippet1> 
    End Sub

    Private Sub ShowDecimal()
        ' <Snippet2>
        Dim value As Integer

        value = 12345
        Console.WriteLine(value.ToString("D"))
        ' Displays 12345   
        Console.WriteLine(value.ToString("D8"))
        ' Displays 00012345

        value = -12345
        Console.WriteLine(value.ToString("D"))
        ' Displays -12345
        Console.WriteLine(value.ToString("D8"))
        ' Displays -00012345
        ' </Snippet2>
    End Sub

    Private Sub ShowExponentiation()
        ' <Snippet3>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture))
        ' Displays 1.234568E+004

        Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture))
        ' Displays 1.2345678900E+004

        Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture))
        ' Displays 1.2346e+004

        Console.WriteLine(value.ToString("E", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 1,234568E+004
        ' </Snippet3>
    End Sub

    Private Sub ShowFixedPoint()
        ' <Snippet4>
        Dim integerNumber As Integer
        integerNumber = 17843
        Console.WriteLine(integerNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 17843.00

        integerNumber = -29541
        Console.WriteLine(integerNumber.ToString("F3", CultureInfo.InvariantCulture))
        ' Displays -29541.000

        Dim doubleNumber As Double
        doubleNumber = 18934.1879
        Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 18934.19

        Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture))
        ' Displays 18934

        doubleNumber = -1898300.1987
        Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture))
        ' Displays -1898300.2

        Console.WriteLine(doubleNumber.ToString("F3", _
                          CultureInfo.CreateSpecificCulture("es-ES")))
        ' Displays -1898300,199                        
        ' </Snippet4>
    End Sub

    Private Sub ShowGeneral()
        ' <Snippet5>
        Dim number As Double

        number = 12345.6789
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays  12345.6789
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 12345,6789

        Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture))
        ' Displays 12345.68 

        number = .0000023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 2.3E-06       
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 2,3E-06

        number = .0023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 0.0023

        number = 1234
        Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture))
        ' Displays 1.2E+03

        number = Math.Pi
        Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture))
        ' Displays 3.1416    
        ' </Snippet5>
    End Sub

    Private Sub ShowNumeric()
        ' <Snippet6>
        Dim dblValue As Double = -12445.6789
        Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
        ' Displays -12,445.68
        Console.WriteLine(dblValue.ToString("N1", _
                          CultureInfo.CreateSpecificCulture("sv-SE")))
        ' Displays -12 445,7

        Dim intValue As Integer = 123456789
        Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture))
        ' Displays 123,456,789.0 
        ' </Snippet6>
    End Sub

    Private Sub ShowPercent()
        ' <Snippet7>
        Dim number As Double = .2468013
        Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture))
        ' Displays 24.68 %
        Console.WriteLine(number.ToString("P", _
                          CultureInfo.CreateSpecificCulture("hr-HR")))
        ' Displays 24,68%     
        Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture))
        ' Displays 24.7 %
        ' </Snippet7>
    End Sub

    Private Sub ShowRoundTrip()
        ' <Snippet8>
        Dim value As Double

        value = Math.Pi
        Console.WriteLine(value.ToString("r"))
        ' Displays 3.1415926535897931
        Console.WriteLine(value.ToString("r", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 3,1415926535897931
        value = 1.623e-21
        Console.WriteLine(value.ToString("r"))
        ' Displays 1.623E-21
        ' </Snippet8>
    End Sub

    Private Sub ShowHex()
        ' <Snippet9>
        Dim value As Integer

        value = &h2045e
        Console.WriteLine(value.ToString("x"))
        ' Displays 2045e
        Console.WriteLine(value.ToString("X"))
        ' Displays 2045E
        Console.WriteLine(value.ToString("X8"))
        ' Displays 0002045E

        value = 123456789
        Console.WriteLine(value.ToString("X"))
        ' Displays 75BCD15
        Console.WriteLine(value.ToString("X2"))
        ' Displays 75BCD15
        ' </Snippet9>
    End Sub

    Private Sub ShowInvalid()
        Dim value As Integer = 12
        Dim specifier AS String = "Z"
        Try
            Console.WriteLine(value.ToString("Z"))
        Catch e As Exception
            Console.WriteLine("{0}: {1} is not a valid format specifier.", _
                              e.GetType().Name, specifier)
        End Try
    End Sub
End Module

General format specifier (G)

The general ("G") format specifier converts a number to the more compact of either fixed-point or scientific notation, depending on the type of the number and whether a precision specifier is present. The precision specifier defines the maximum number of significant digits that can appear in the result string. If the precision specifier is omitted or zero, the type of the number determines the default precision, as indicated in the following table.

Numeric type Default precision
System.Byte or System.SByte 3 digits
System.Int16 or System.UInt16 5 digits
System.Int32 or System.UInt32 10 digits
System.Int64 19 digits
System.UInt64 20 digits
System.Numerics.BigInteger Unlimited (same as "R")
System.Half Smallest round-trippable number of digits to represent the number
System.Single Smallest round-trippable number of digits to represent the number (in .NET Framework, G7 is the default)
System.Double Smallest round-trippable number of digits to represent the number (in .NET Framework, G15 is the default)
System.Decimal Smallest round-trippable number of digits to represent the number

Fixed-point notation is used if the exponent that would result from expressing the number in scientific notation is greater than -5 and less than the precision specifier; otherwise, scientific notation is used. The result contains a decimal point if required, and trailing zeros after the decimal point are omitted. If the precision specifier is present and the number of significant digits in the result exceeds the specified precision, the excess trailing digits are removed by rounding.

However, if the number is a System.Decimal and the precision specifier is omitted, fixed-point notation is always used and trailing zeros are preserved.

If scientific notation is used, the exponent in the result is prefixed with "E" if the format specifier is "G", or "e" if the format specifier is "g". The exponent contains a minimum of two digits. This differs from the format for scientific notation that is produced by the exponential format specifier, which includes a minimum of three digits in the exponent.

When used with a System.Double value, the "G17" format specifier ensures that the original System.Double value successfully round-trips. This is because System.Double is an IEEE 754-2008-compliant double-precision (binary64) floating-point number that gives up to 17 significant digits of precision. On .NET Framework, we recommend its use instead of the "R" format specifier, since in some cases "R" fails to successfully round-trip double-precision floating point values.

When used with a System.Single value, the "G9" format specifier ensures that the original System.Single value successfully round-trips. This is because System.Single is an IEEE 754-2008-compliant single-precision (binary32) floating-point number that gives up to nine significant digits of precision. For performance reasons, we recommend its use instead of the "R" format specifier.

The result string is affected by the formatting information of the current System.Globalization.NumberFormatInfo object. The following table lists the System.Globalization.NumberFormatInfo properties that control the formatting of the result string.

NumberFormatInfo property Description
System.Globalization.NumberFormatInfo.NegativeSign Defines the string that indicates that a number is negative.
System.Globalization.NumberFormatInfo.NumberDecimalSeparator Defines the string that separates integral digits from decimal digits.
System.Globalization.NumberFormatInfo.PositiveSign Defines the string that indicates that an exponent is positive.

The following example formats assorted floating-point values with the general format specifier:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.Numeric.Standard/cs/Standard.cs5

csharp
using System;
using System.Globalization;

class formatting
{
   public static void Main()
   {
      Console.Clear();
      Console.WriteLine(CultureInfo.CurrentUICulture.Name);
      Console.WriteLine(CultureInfo.CurrentCulture.Name);
      Console.WriteLine();
      Console.WriteLine("Currency Format Specifier:");
      ShowCurrency();
      Console.WriteLine();
      Console.WriteLine("Decimal Format Specifier:");
      ShowDecimal();
      Console.WriteLine();
      Console.WriteLine("Exponentiation Format Specifier:");
      ShowExponentiation();
      Console.WriteLine();
      Console.WriteLine("Fixed Point Format Specifier:");
      ShowFixedPoint();
      Console.WriteLine();
      Console.WriteLine("'G' Format Specifier:");
      ShowGeneral();
      Console.WriteLine();
      Console.WriteLine("'N' Format Specifier:");
      ShowNumeric();
      Console.WriteLine();
      Console.WriteLine("Percent Format Specifier:");
      ShowPercent();
      Console.WriteLine();
      Console.WriteLine("Round-trip Format Specifier:");
      ShowRoundTrip();
      Console.WriteLine();
      Console.WriteLine("Hexadecimal Format Specifier:");
      ShowHex();
   }

   public static void ShowCurrency()
   {
      // <Snippet1>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3",
                        CultureInfo.CreateSpecificCulture("da-DK")));
      // The example displays the following output on a system whose
      // current culture is English (United States):
      //       $12,345.68
      //       $12,345.679
      //       12.345,679 kr
      // </Snippet1>
   }

   public static void ShowDecimal()
   {
      // <Snippet2>
      int value;

      value = 12345;
      Console.WriteLine(value.ToString("D"));
      // Displays 12345
      Console.WriteLine(value.ToString("D8"));
      // Displays 00012345

      value = -12345;
      Console.WriteLine(value.ToString("D"));
      // Displays -12345
      Console.WriteLine(value.ToString("D8"));
      // Displays -00012345
      // </Snippet2>
   }

   public static void ShowExponentiation()
   {
      // <Snippet3>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture));
      // Displays 1.234568E+004

      Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture));
      // Displays 1.2345678900E+004

      Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture));
      // Displays 1.2346e+004

      Console.WriteLine(value.ToString("E",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 1,234568E+004
      // </Snippet3>
   }

   public static void ShowFixedPoint()
   {
      // <Snippet4>
      int integerNumber;
      integerNumber = 17843;
      Console.WriteLine(integerNumber.ToString("F",
                        CultureInfo.InvariantCulture));
      // Displays 17843.00

      integerNumber = -29541;
      Console.WriteLine(integerNumber.ToString("F3",
                        CultureInfo.InvariantCulture));
      // Displays -29541.000

      double doubleNumber;
      doubleNumber = 18934.1879;
      Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture));
      // Displays 18934.19

      Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture));
      // Displays 18934

      doubleNumber = -1898300.1987;
      Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture));
      // Displays -1898300.2

      Console.WriteLine(doubleNumber.ToString("F3",
                        CultureInfo.CreateSpecificCulture("es-ES")));
      // Displays -1898300,199
      // </Snippet4>
   }

   public static void ShowGeneral()
   {
      // <Snippet5>
      double number;

      number = 12345.6789;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays  12345.6789
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 12345,6789

      Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture));
      // Displays 12345.68

      number = .0000023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 2.3E-06
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 2,3E-06

      number = .0023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 0.0023

      number = 1234;
      Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture));
      // Displays 1.2E+03

      number = Math.PI;
      Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture));
      // Displays 3.1416
      // </Snippet5>
   }

   public static void ShowNumeric()
   {
      // <Snippet6>
      double dblValue = -12445.6789;
      Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture));
      // Displays -12,445.68
      Console.WriteLine(dblValue.ToString("N1",
                        CultureInfo.CreateSpecificCulture("sv-SE")));
      // Displays -12 445,7

      int intValue = 123456789;
      Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture));
      // Displays 123,456,789.0
      // </Snippet6>
   }

   public static void ShowPercent()
   {
      // <Snippet7>
      double number = .2468013;
      Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture));
      // Displays 24.68 %
      Console.WriteLine(number.ToString("P",
                        CultureInfo.CreateSpecificCulture("hr-HR")));
      // Displays 24,68%
      Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture));
      // Displays 24.7 %
      // </Snippet7>
   }

   public static void ShowRoundTrip()
   {
      // <Snippet8>
      double value;

      value = Math.PI;
      Console.WriteLine(value.ToString("r"));
      // Displays 3.1415926535897931
      Console.WriteLine(value.ToString("r",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 3,1415926535897931
      value = 1.623e-21;
      Console.WriteLine(value.ToString("r"));
      // Displays 1.623E-21
      // </Snippet8>
   }

   public static void ShowHex()
   {
      // <Snippet9>
      int value;

      value = 0x2045e;
      Console.WriteLine(value.ToString("x"));
      // Displays 2045e
      Console.WriteLine(value.ToString("X"));
      // Displays 2045E
      Console.WriteLine(value.ToString("X8"));
      // Displays 0002045E

      value = 123456789;
      Console.WriteLine(value.ToString("X"));
      // Displays 75BCD15
      Console.WriteLine(value.ToString("X2"));
      // Displays 75BCD15
      // </Snippet9>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.Numeric.Standard/vb/Standard.vb5

vb
' Visual Basic .NET Document
Option Strict On

Imports System.Globalization

Module modMain

    Public Sub Main()
        Console.Clear()
        Console.WriteLine(CultureInfo.CurrentUICulture.Name)
        Console.WriteLine(CultureInfo.CurrentCulture.Name)
        Console.WriteLine()
        Console.WriteLine("Currency Format Specifier:")
        ShowCurrency()
        Console.WriteLine()
        Console.WriteLine("Decimal Format Specifier:")
        ShowDecimal()
        Console.WriteLine()
        Console.WriteLine("Exponentiation Format Specifier:")
        ShowExponentiation()
        Console.WriteLine()
        Console.WriteLine("Fixed Point Format Specifier:")
        ShowFixedPoint()
        Console.WriteLine()
        Console.WriteLine("'G' Format Specifier:")
        ShowGeneral()
        Console.WriteLine()
        Console.WriteLine("'N' Format Specifier:")
        ShowNumeric()
        Console.WriteLine()
        Console.WriteLine("Percent Format Specifier:")
        ShowPercent()
        Console.WriteLine()
        Console.WriteLine("Round-trip Format Specifier:")
        ShowRoundTrip()
        Console.WriteLine()
        Console.WriteLine("Hexadecimal Format Specifier:")
        ShowHex()
        Console.WriteLine()
        Console.WriteLine("Invalid Format Specifier")
        ShowInvalid()
    End Sub

    Private Sub ShowCurrency()
        ' <Snippet1>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", _
                          CultureInfo.CreateSpecificCulture("da-DK")))
        ' The example displays the following output on a system whose
        ' current culture is English (United States):
        '       $12,345.68
        '       $12,345.679
        '       kr 12.345,679
        ' </Snippet1> 
    End Sub

    Private Sub ShowDecimal()
        ' <Snippet2>
        Dim value As Integer

        value = 12345
        Console.WriteLine(value.ToString("D"))
        ' Displays 12345   
        Console.WriteLine(value.ToString("D8"))
        ' Displays 00012345

        value = -12345
        Console.WriteLine(value.ToString("D"))
        ' Displays -12345
        Console.WriteLine(value.ToString("D8"))
        ' Displays -00012345
        ' </Snippet2>
    End Sub

    Private Sub ShowExponentiation()
        ' <Snippet3>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture))
        ' Displays 1.234568E+004

        Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture))
        ' Displays 1.2345678900E+004

        Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture))
        ' Displays 1.2346e+004

        Console.WriteLine(value.ToString("E", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 1,234568E+004
        ' </Snippet3>
    End Sub

    Private Sub ShowFixedPoint()
        ' <Snippet4>
        Dim integerNumber As Integer
        integerNumber = 17843
        Console.WriteLine(integerNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 17843.00

        integerNumber = -29541
        Console.WriteLine(integerNumber.ToString("F3", CultureInfo.InvariantCulture))
        ' Displays -29541.000

        Dim doubleNumber As Double
        doubleNumber = 18934.1879
        Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 18934.19

        Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture))
        ' Displays 18934

        doubleNumber = -1898300.1987
        Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture))
        ' Displays -1898300.2

        Console.WriteLine(doubleNumber.ToString("F3", _
                          CultureInfo.CreateSpecificCulture("es-ES")))
        ' Displays -1898300,199                        
        ' </Snippet4>
    End Sub

    Private Sub ShowGeneral()
        ' <Snippet5>
        Dim number As Double

        number = 12345.6789
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays  12345.6789
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 12345,6789

        Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture))
        ' Displays 12345.68 

        number = .0000023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 2.3E-06       
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 2,3E-06

        number = .0023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 0.0023

        number = 1234
        Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture))
        ' Displays 1.2E+03

        number = Math.Pi
        Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture))
        ' Displays 3.1416    
        ' </Snippet5>
    End Sub

    Private Sub ShowNumeric()
        ' <Snippet6>
        Dim dblValue As Double = -12445.6789
        Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
        ' Displays -12,445.68
        Console.WriteLine(dblValue.ToString("N1", _
                          CultureInfo.CreateSpecificCulture("sv-SE")))
        ' Displays -12 445,7

        Dim intValue As Integer = 123456789
        Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture))
        ' Displays 123,456,789.0 
        ' </Snippet6>
    End Sub

    Private Sub ShowPercent()
        ' <Snippet7>
        Dim number As Double = .2468013
        Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture))
        ' Displays 24.68 %
        Console.WriteLine(number.ToString("P", _
                          CultureInfo.CreateSpecificCulture("hr-HR")))
        ' Displays 24,68%     
        Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture))
        ' Displays 24.7 %
        ' </Snippet7>
    End Sub

    Private Sub ShowRoundTrip()
        ' <Snippet8>
        Dim value As Double

        value = Math.Pi
        Console.WriteLine(value.ToString("r"))
        ' Displays 3.1415926535897931
        Console.WriteLine(value.ToString("r", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 3,1415926535897931
        value = 1.623e-21
        Console.WriteLine(value.ToString("r"))
        ' Displays 1.623E-21
        ' </Snippet8>
    End Sub

    Private Sub ShowHex()
        ' <Snippet9>
        Dim value As Integer

        value = &h2045e
        Console.WriteLine(value.ToString("x"))
        ' Displays 2045e
        Console.WriteLine(value.ToString("X"))
        ' Displays 2045E
        Console.WriteLine(value.ToString("X8"))
        ' Displays 0002045E

        value = 123456789
        Console.WriteLine(value.ToString("X"))
        ' Displays 75BCD15
        Console.WriteLine(value.ToString("X2"))
        ' Displays 75BCD15
        ' </Snippet9>
    End Sub

    Private Sub ShowInvalid()
        Dim value As Integer = 12
        Dim specifier AS String = "Z"
        Try
            Console.WriteLine(value.ToString("Z"))
        Catch e As Exception
            Console.WriteLine("{0}: {1} is not a valid format specifier.", _
                              e.GetType().Name, specifier)
        End Try
    End Sub
End Module

Numeric format specifier (N)

The numeric ("N") format specifier converts a number to a string of the form "-d,ddd,ddd.ddd…", where "-" indicates a negative number symbol if required, "d" indicates a digit (0-9), "," indicates a group separator, and "." indicates a decimal point symbol. The precision specifier indicates the desired number of digits after the decimal point. If the precision specifier is omitted, the number of decimal places is defined by the current System.Globalization.NumberFormatInfo.NumberDecimalDigits property.

The result string is affected by the formatting information of the current System.Globalization.NumberFormatInfo object. The following table lists the System.Globalization.NumberFormatInfo properties that control the formatting of the result string.

NumberFormatInfo property Description
System.Globalization.NumberFormatInfo.NegativeSign Defines the string that indicates that a number is negative.
System.Globalization.NumberFormatInfo.NumberNegativePattern Defines the format of negative values, and specifies whether the negative sign is represented by parentheses or the System.Globalization.NumberFormatInfo.NegativeSign property.
System.Globalization.NumberFormatInfo.NumberGroupSizes Defines the number of integral digits that appear between group separators.
System.Globalization.NumberFormatInfo.NumberGroupSeparator Defines the string that separates groups of integral numbers.
System.Globalization.NumberFormatInfo.NumberDecimalSeparator Defines the string that separates integral and decimal digits.
System.Globalization.NumberFormatInfo.NumberDecimalDigits Defines the default number of decimal digits. This value can be overridden by using a precision specifier.

The following example formats assorted floating-point values with the number format specifier:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.Numeric.Standard/cs/Standard.cs6

csharp
using System;
using System.Globalization;

class formatting
{
   public static void Main()
   {
      Console.Clear();
      Console.WriteLine(CultureInfo.CurrentUICulture.Name);
      Console.WriteLine(CultureInfo.CurrentCulture.Name);
      Console.WriteLine();
      Console.WriteLine("Currency Format Specifier:");
      ShowCurrency();
      Console.WriteLine();
      Console.WriteLine("Decimal Format Specifier:");
      ShowDecimal();
      Console.WriteLine();
      Console.WriteLine("Exponentiation Format Specifier:");
      ShowExponentiation();
      Console.WriteLine();
      Console.WriteLine("Fixed Point Format Specifier:");
      ShowFixedPoint();
      Console.WriteLine();
      Console.WriteLine("'G' Format Specifier:");
      ShowGeneral();
      Console.WriteLine();
      Console.WriteLine("'N' Format Specifier:");
      ShowNumeric();
      Console.WriteLine();
      Console.WriteLine("Percent Format Specifier:");
      ShowPercent();
      Console.WriteLine();
      Console.WriteLine("Round-trip Format Specifier:");
      ShowRoundTrip();
      Console.WriteLine();
      Console.WriteLine("Hexadecimal Format Specifier:");
      ShowHex();
   }

   public static void ShowCurrency()
   {
      // <Snippet1>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3",
                        CultureInfo.CreateSpecificCulture("da-DK")));
      // The example displays the following output on a system whose
      // current culture is English (United States):
      //       $12,345.68
      //       $12,345.679
      //       12.345,679 kr
      // </Snippet1>
   }

   public static void ShowDecimal()
   {
      // <Snippet2>
      int value;

      value = 12345;
      Console.WriteLine(value.ToString("D"));
      // Displays 12345
      Console.WriteLine(value.ToString("D8"));
      // Displays 00012345

      value = -12345;
      Console.WriteLine(value.ToString("D"));
      // Displays -12345
      Console.WriteLine(value.ToString("D8"));
      // Displays -00012345
      // </Snippet2>
   }

   public static void ShowExponentiation()
   {
      // <Snippet3>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture));
      // Displays 1.234568E+004

      Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture));
      // Displays 1.2345678900E+004

      Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture));
      // Displays 1.2346e+004

      Console.WriteLine(value.ToString("E",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 1,234568E+004
      // </Snippet3>
   }

   public static void ShowFixedPoint()
   {
      // <Snippet4>
      int integerNumber;
      integerNumber = 17843;
      Console.WriteLine(integerNumber.ToString("F",
                        CultureInfo.InvariantCulture));
      // Displays 17843.00

      integerNumber = -29541;
      Console.WriteLine(integerNumber.ToString("F3",
                        CultureInfo.InvariantCulture));
      // Displays -29541.000

      double doubleNumber;
      doubleNumber = 18934.1879;
      Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture));
      // Displays 18934.19

      Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture));
      // Displays 18934

      doubleNumber = -1898300.1987;
      Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture));
      // Displays -1898300.2

      Console.WriteLine(doubleNumber.ToString("F3",
                        CultureInfo.CreateSpecificCulture("es-ES")));
      // Displays -1898300,199
      // </Snippet4>
   }

   public static void ShowGeneral()
   {
      // <Snippet5>
      double number;

      number = 12345.6789;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays  12345.6789
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 12345,6789

      Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture));
      // Displays 12345.68

      number = .0000023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 2.3E-06
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 2,3E-06

      number = .0023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 0.0023

      number = 1234;
      Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture));
      // Displays 1.2E+03

      number = Math.PI;
      Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture));
      // Displays 3.1416
      // </Snippet5>
   }

   public static void ShowNumeric()
   {
      // <Snippet6>
      double dblValue = -12445.6789;
      Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture));
      // Displays -12,445.68
      Console.WriteLine(dblValue.ToString("N1",
                        CultureInfo.CreateSpecificCulture("sv-SE")));
      // Displays -12 445,7

      int intValue = 123456789;
      Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture));
      // Displays 123,456,789.0
      // </Snippet6>
   }

   public static void ShowPercent()
   {
      // <Snippet7>
      double number = .2468013;
      Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture));
      // Displays 24.68 %
      Console.WriteLine(number.ToString("P",
                        CultureInfo.CreateSpecificCulture("hr-HR")));
      // Displays 24,68%
      Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture));
      // Displays 24.7 %
      // </Snippet7>
   }

   public static void ShowRoundTrip()
   {
      // <Snippet8>
      double value;

      value = Math.PI;
      Console.WriteLine(value.ToString("r"));
      // Displays 3.1415926535897931
      Console.WriteLine(value.ToString("r",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 3,1415926535897931
      value = 1.623e-21;
      Console.WriteLine(value.ToString("r"));
      // Displays 1.623E-21
      // </Snippet8>
   }

   public static void ShowHex()
   {
      // <Snippet9>
      int value;

      value = 0x2045e;
      Console.WriteLine(value.ToString("x"));
      // Displays 2045e
      Console.WriteLine(value.ToString("X"));
      // Displays 2045E
      Console.WriteLine(value.ToString("X8"));
      // Displays 0002045E

      value = 123456789;
      Console.WriteLine(value.ToString("X"));
      // Displays 75BCD15
      Console.WriteLine(value.ToString("X2"));
      // Displays 75BCD15
      // </Snippet9>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.Numeric.Standard/vb/Standard.vb6

vb
' Visual Basic .NET Document
Option Strict On

Imports System.Globalization

Module modMain

    Public Sub Main()
        Console.Clear()
        Console.WriteLine(CultureInfo.CurrentUICulture.Name)
        Console.WriteLine(CultureInfo.CurrentCulture.Name)
        Console.WriteLine()
        Console.WriteLine("Currency Format Specifier:")
        ShowCurrency()
        Console.WriteLine()
        Console.WriteLine("Decimal Format Specifier:")
        ShowDecimal()
        Console.WriteLine()
        Console.WriteLine("Exponentiation Format Specifier:")
        ShowExponentiation()
        Console.WriteLine()
        Console.WriteLine("Fixed Point Format Specifier:")
        ShowFixedPoint()
        Console.WriteLine()
        Console.WriteLine("'G' Format Specifier:")
        ShowGeneral()
        Console.WriteLine()
        Console.WriteLine("'N' Format Specifier:")
        ShowNumeric()
        Console.WriteLine()
        Console.WriteLine("Percent Format Specifier:")
        ShowPercent()
        Console.WriteLine()
        Console.WriteLine("Round-trip Format Specifier:")
        ShowRoundTrip()
        Console.WriteLine()
        Console.WriteLine("Hexadecimal Format Specifier:")
        ShowHex()
        Console.WriteLine()
        Console.WriteLine("Invalid Format Specifier")
        ShowInvalid()
    End Sub

    Private Sub ShowCurrency()
        ' <Snippet1>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", _
                          CultureInfo.CreateSpecificCulture("da-DK")))
        ' The example displays the following output on a system whose
        ' current culture is English (United States):
        '       $12,345.68
        '       $12,345.679
        '       kr 12.345,679
        ' </Snippet1> 
    End Sub

    Private Sub ShowDecimal()
        ' <Snippet2>
        Dim value As Integer

        value = 12345
        Console.WriteLine(value.ToString("D"))
        ' Displays 12345   
        Console.WriteLine(value.ToString("D8"))
        ' Displays 00012345

        value = -12345
        Console.WriteLine(value.ToString("D"))
        ' Displays -12345
        Console.WriteLine(value.ToString("D8"))
        ' Displays -00012345
        ' </Snippet2>
    End Sub

    Private Sub ShowExponentiation()
        ' <Snippet3>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture))
        ' Displays 1.234568E+004

        Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture))
        ' Displays 1.2345678900E+004

        Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture))
        ' Displays 1.2346e+004

        Console.WriteLine(value.ToString("E", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 1,234568E+004
        ' </Snippet3>
    End Sub

    Private Sub ShowFixedPoint()
        ' <Snippet4>
        Dim integerNumber As Integer
        integerNumber = 17843
        Console.WriteLine(integerNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 17843.00

        integerNumber = -29541
        Console.WriteLine(integerNumber.ToString("F3", CultureInfo.InvariantCulture))
        ' Displays -29541.000

        Dim doubleNumber As Double
        doubleNumber = 18934.1879
        Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 18934.19

        Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture))
        ' Displays 18934

        doubleNumber = -1898300.1987
        Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture))
        ' Displays -1898300.2

        Console.WriteLine(doubleNumber.ToString("F3", _
                          CultureInfo.CreateSpecificCulture("es-ES")))
        ' Displays -1898300,199                        
        ' </Snippet4>
    End Sub

    Private Sub ShowGeneral()
        ' <Snippet5>
        Dim number As Double

        number = 12345.6789
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays  12345.6789
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 12345,6789

        Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture))
        ' Displays 12345.68 

        number = .0000023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 2.3E-06       
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 2,3E-06

        number = .0023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 0.0023

        number = 1234
        Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture))
        ' Displays 1.2E+03

        number = Math.Pi
        Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture))
        ' Displays 3.1416    
        ' </Snippet5>
    End Sub

    Private Sub ShowNumeric()
        ' <Snippet6>
        Dim dblValue As Double = -12445.6789
        Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
        ' Displays -12,445.68
        Console.WriteLine(dblValue.ToString("N1", _
                          CultureInfo.CreateSpecificCulture("sv-SE")))
        ' Displays -12 445,7

        Dim intValue As Integer = 123456789
        Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture))
        ' Displays 123,456,789.0 
        ' </Snippet6>
    End Sub

    Private Sub ShowPercent()
        ' <Snippet7>
        Dim number As Double = .2468013
        Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture))
        ' Displays 24.68 %
        Console.WriteLine(number.ToString("P", _
                          CultureInfo.CreateSpecificCulture("hr-HR")))
        ' Displays 24,68%     
        Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture))
        ' Displays 24.7 %
        ' </Snippet7>
    End Sub

    Private Sub ShowRoundTrip()
        ' <Snippet8>
        Dim value As Double

        value = Math.Pi
        Console.WriteLine(value.ToString("r"))
        ' Displays 3.1415926535897931
        Console.WriteLine(value.ToString("r", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 3,1415926535897931
        value = 1.623e-21
        Console.WriteLine(value.ToString("r"))
        ' Displays 1.623E-21
        ' </Snippet8>
    End Sub

    Private Sub ShowHex()
        ' <Snippet9>
        Dim value As Integer

        value = &h2045e
        Console.WriteLine(value.ToString("x"))
        ' Displays 2045e
        Console.WriteLine(value.ToString("X"))
        ' Displays 2045E
        Console.WriteLine(value.ToString("X8"))
        ' Displays 0002045E

        value = 123456789
        Console.WriteLine(value.ToString("X"))
        ' Displays 75BCD15
        Console.WriteLine(value.ToString("X2"))
        ' Displays 75BCD15
        ' </Snippet9>
    End Sub

    Private Sub ShowInvalid()
        Dim value As Integer = 12
        Dim specifier AS String = "Z"
        Try
            Console.WriteLine(value.ToString("Z"))
        Catch e As Exception
            Console.WriteLine("{0}: {1} is not a valid format specifier.", _
                              e.GetType().Name, specifier)
        End Try
    End Sub
End Module

Percent format specifier (P)

The percent ("P") format specifier multiplies a number by 100 and converts it to a string that represents a percentage. The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default numeric precision supplied by the current System.Globalization.NumberFormatInfo.PercentDecimalDigits property is used.

The following table lists the System.Globalization.NumberFormatInfo properties that control the formatting of the returned string.

NumberFormatInfo property Description
System.Globalization.NumberFormatInfo.PercentPositivePattern Defines the placement of the percent symbol for positive values.
System.Globalization.NumberFormatInfo.PercentNegativePattern Defines the placement of the percent symbol and the negative symbol for negative values.
System.Globalization.NumberFormatInfo.NegativeSign Defines the string that indicates that a number is negative.
System.Globalization.NumberFormatInfo.PercentSymbol Defines the percent symbol.
System.Globalization.NumberFormatInfo.PercentDecimalDigits Defines the default number of decimal digits in a percentage value. This value can be overridden by using the precision specifier.
System.Globalization.NumberFormatInfo.PercentDecimalSeparator Defines the string that separates integral and decimal digits.
System.Globalization.NumberFormatInfo.PercentGroupSeparator Defines the string that separates groups of integral numbers.
System.Globalization.NumberFormatInfo.PercentGroupSizes Defines the number of integer digits that appear in a group.

The following example formats floating-point values with the percent format specifier:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.Numeric.Standard/cs/Standard.cs7

csharp
using System;
using System.Globalization;

class formatting
{
   public static void Main()
   {
      Console.Clear();
      Console.WriteLine(CultureInfo.CurrentUICulture.Name);
      Console.WriteLine(CultureInfo.CurrentCulture.Name);
      Console.WriteLine();
      Console.WriteLine("Currency Format Specifier:");
      ShowCurrency();
      Console.WriteLine();
      Console.WriteLine("Decimal Format Specifier:");
      ShowDecimal();
      Console.WriteLine();
      Console.WriteLine("Exponentiation Format Specifier:");
      ShowExponentiation();
      Console.WriteLine();
      Console.WriteLine("Fixed Point Format Specifier:");
      ShowFixedPoint();
      Console.WriteLine();
      Console.WriteLine("'G' Format Specifier:");
      ShowGeneral();
      Console.WriteLine();
      Console.WriteLine("'N' Format Specifier:");
      ShowNumeric();
      Console.WriteLine();
      Console.WriteLine("Percent Format Specifier:");
      ShowPercent();
      Console.WriteLine();
      Console.WriteLine("Round-trip Format Specifier:");
      ShowRoundTrip();
      Console.WriteLine();
      Console.WriteLine("Hexadecimal Format Specifier:");
      ShowHex();
   }

   public static void ShowCurrency()
   {
      // <Snippet1>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3",
                        CultureInfo.CreateSpecificCulture("da-DK")));
      // The example displays the following output on a system whose
      // current culture is English (United States):
      //       $12,345.68
      //       $12,345.679
      //       12.345,679 kr
      // </Snippet1>
   }

   public static void ShowDecimal()
   {
      // <Snippet2>
      int value;

      value = 12345;
      Console.WriteLine(value.ToString("D"));
      // Displays 12345
      Console.WriteLine(value.ToString("D8"));
      // Displays 00012345

      value = -12345;
      Console.WriteLine(value.ToString("D"));
      // Displays -12345
      Console.WriteLine(value.ToString("D8"));
      // Displays -00012345
      // </Snippet2>
   }

   public static void ShowExponentiation()
   {
      // <Snippet3>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture));
      // Displays 1.234568E+004

      Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture));
      // Displays 1.2345678900E+004

      Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture));
      // Displays 1.2346e+004

      Console.WriteLine(value.ToString("E",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 1,234568E+004
      // </Snippet3>
   }

   public static void ShowFixedPoint()
   {
      // <Snippet4>
      int integerNumber;
      integerNumber = 17843;
      Console.WriteLine(integerNumber.ToString("F",
                        CultureInfo.InvariantCulture));
      // Displays 17843.00

      integerNumber = -29541;
      Console.WriteLine(integerNumber.ToString("F3",
                        CultureInfo.InvariantCulture));
      // Displays -29541.000

      double doubleNumber;
      doubleNumber = 18934.1879;
      Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture));
      // Displays 18934.19

      Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture));
      // Displays 18934

      doubleNumber = -1898300.1987;
      Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture));
      // Displays -1898300.2

      Console.WriteLine(doubleNumber.ToString("F3",
                        CultureInfo.CreateSpecificCulture("es-ES")));
      // Displays -1898300,199
      // </Snippet4>
   }

   public static void ShowGeneral()
   {
      // <Snippet5>
      double number;

      number = 12345.6789;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays  12345.6789
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 12345,6789

      Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture));
      // Displays 12345.68

      number = .0000023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 2.3E-06
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 2,3E-06

      number = .0023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 0.0023

      number = 1234;
      Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture));
      // Displays 1.2E+03

      number = Math.PI;
      Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture));
      // Displays 3.1416
      // </Snippet5>
   }

   public static void ShowNumeric()
   {
      // <Snippet6>
      double dblValue = -12445.6789;
      Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture));
      // Displays -12,445.68
      Console.WriteLine(dblValue.ToString("N1",
                        CultureInfo.CreateSpecificCulture("sv-SE")));
      // Displays -12 445,7

      int intValue = 123456789;
      Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture));
      // Displays 123,456,789.0
      // </Snippet6>
   }

   public static void ShowPercent()
   {
      // <Snippet7>
      double number = .2468013;
      Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture));
      // Displays 24.68 %
      Console.WriteLine(number.ToString("P",
                        CultureInfo.CreateSpecificCulture("hr-HR")));
      // Displays 24,68%
      Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture));
      // Displays 24.7 %
      // </Snippet7>
   }

   public static void ShowRoundTrip()
   {
      // <Snippet8>
      double value;

      value = Math.PI;
      Console.WriteLine(value.ToString("r"));
      // Displays 3.1415926535897931
      Console.WriteLine(value.ToString("r",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 3,1415926535897931
      value = 1.623e-21;
      Console.WriteLine(value.ToString("r"));
      // Displays 1.623E-21
      // </Snippet8>
   }

   public static void ShowHex()
   {
      // <Snippet9>
      int value;

      value = 0x2045e;
      Console.WriteLine(value.ToString("x"));
      // Displays 2045e
      Console.WriteLine(value.ToString("X"));
      // Displays 2045E
      Console.WriteLine(value.ToString("X8"));
      // Displays 0002045E

      value = 123456789;
      Console.WriteLine(value.ToString("X"));
      // Displays 75BCD15
      Console.WriteLine(value.ToString("X2"));
      // Displays 75BCD15
      // </Snippet9>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.Numeric.Standard/vb/Standard.vb7

vb
' Visual Basic .NET Document
Option Strict On

Imports System.Globalization

Module modMain

    Public Sub Main()
        Console.Clear()
        Console.WriteLine(CultureInfo.CurrentUICulture.Name)
        Console.WriteLine(CultureInfo.CurrentCulture.Name)
        Console.WriteLine()
        Console.WriteLine("Currency Format Specifier:")
        ShowCurrency()
        Console.WriteLine()
        Console.WriteLine("Decimal Format Specifier:")
        ShowDecimal()
        Console.WriteLine()
        Console.WriteLine("Exponentiation Format Specifier:")
        ShowExponentiation()
        Console.WriteLine()
        Console.WriteLine("Fixed Point Format Specifier:")
        ShowFixedPoint()
        Console.WriteLine()
        Console.WriteLine("'G' Format Specifier:")
        ShowGeneral()
        Console.WriteLine()
        Console.WriteLine("'N' Format Specifier:")
        ShowNumeric()
        Console.WriteLine()
        Console.WriteLine("Percent Format Specifier:")
        ShowPercent()
        Console.WriteLine()
        Console.WriteLine("Round-trip Format Specifier:")
        ShowRoundTrip()
        Console.WriteLine()
        Console.WriteLine("Hexadecimal Format Specifier:")
        ShowHex()
        Console.WriteLine()
        Console.WriteLine("Invalid Format Specifier")
        ShowInvalid()
    End Sub

    Private Sub ShowCurrency()
        ' <Snippet1>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", _
                          CultureInfo.CreateSpecificCulture("da-DK")))
        ' The example displays the following output on a system whose
        ' current culture is English (United States):
        '       $12,345.68
        '       $12,345.679
        '       kr 12.345,679
        ' </Snippet1> 
    End Sub

    Private Sub ShowDecimal()
        ' <Snippet2>
        Dim value As Integer

        value = 12345
        Console.WriteLine(value.ToString("D"))
        ' Displays 12345   
        Console.WriteLine(value.ToString("D8"))
        ' Displays 00012345

        value = -12345
        Console.WriteLine(value.ToString("D"))
        ' Displays -12345
        Console.WriteLine(value.ToString("D8"))
        ' Displays -00012345
        ' </Snippet2>
    End Sub

    Private Sub ShowExponentiation()
        ' <Snippet3>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture))
        ' Displays 1.234568E+004

        Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture))
        ' Displays 1.2345678900E+004

        Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture))
        ' Displays 1.2346e+004

        Console.WriteLine(value.ToString("E", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 1,234568E+004
        ' </Snippet3>
    End Sub

    Private Sub ShowFixedPoint()
        ' <Snippet4>
        Dim integerNumber As Integer
        integerNumber = 17843
        Console.WriteLine(integerNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 17843.00

        integerNumber = -29541
        Console.WriteLine(integerNumber.ToString("F3", CultureInfo.InvariantCulture))
        ' Displays -29541.000

        Dim doubleNumber As Double
        doubleNumber = 18934.1879
        Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 18934.19

        Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture))
        ' Displays 18934

        doubleNumber = -1898300.1987
        Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture))
        ' Displays -1898300.2

        Console.WriteLine(doubleNumber.ToString("F3", _
                          CultureInfo.CreateSpecificCulture("es-ES")))
        ' Displays -1898300,199                        
        ' </Snippet4>
    End Sub

    Private Sub ShowGeneral()
        ' <Snippet5>
        Dim number As Double

        number = 12345.6789
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays  12345.6789
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 12345,6789

        Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture))
        ' Displays 12345.68 

        number = .0000023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 2.3E-06       
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 2,3E-06

        number = .0023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 0.0023

        number = 1234
        Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture))
        ' Displays 1.2E+03

        number = Math.Pi
        Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture))
        ' Displays 3.1416    
        ' </Snippet5>
    End Sub

    Private Sub ShowNumeric()
        ' <Snippet6>
        Dim dblValue As Double = -12445.6789
        Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
        ' Displays -12,445.68
        Console.WriteLine(dblValue.ToString("N1", _
                          CultureInfo.CreateSpecificCulture("sv-SE")))
        ' Displays -12 445,7

        Dim intValue As Integer = 123456789
        Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture))
        ' Displays 123,456,789.0 
        ' </Snippet6>
    End Sub

    Private Sub ShowPercent()
        ' <Snippet7>
        Dim number As Double = .2468013
        Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture))
        ' Displays 24.68 %
        Console.WriteLine(number.ToString("P", _
                          CultureInfo.CreateSpecificCulture("hr-HR")))
        ' Displays 24,68%     
        Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture))
        ' Displays 24.7 %
        ' </Snippet7>
    End Sub

    Private Sub ShowRoundTrip()
        ' <Snippet8>
        Dim value As Double

        value = Math.Pi
        Console.WriteLine(value.ToString("r"))
        ' Displays 3.1415926535897931
        Console.WriteLine(value.ToString("r", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 3,1415926535897931
        value = 1.623e-21
        Console.WriteLine(value.ToString("r"))
        ' Displays 1.623E-21
        ' </Snippet8>
    End Sub

    Private Sub ShowHex()
        ' <Snippet9>
        Dim value As Integer

        value = &h2045e
        Console.WriteLine(value.ToString("x"))
        ' Displays 2045e
        Console.WriteLine(value.ToString("X"))
        ' Displays 2045E
        Console.WriteLine(value.ToString("X8"))
        ' Displays 0002045E

        value = 123456789
        Console.WriteLine(value.ToString("X"))
        ' Displays 75BCD15
        Console.WriteLine(value.ToString("X2"))
        ' Displays 75BCD15
        ' </Snippet9>
    End Sub

    Private Sub ShowInvalid()
        Dim value As Integer = 12
        Dim specifier AS String = "Z"
        Try
            Console.WriteLine(value.ToString("Z"))
        Catch e As Exception
            Console.WriteLine("{0}: {1} is not a valid format specifier.", _
                              e.GetType().Name, specifier)
        End Try
    End Sub
End Module

Round-trip format specifier (R)

The round-trip ("R") format specifier attempts to ensure that a numeric value that is converted to a string is parsed back into the same numeric value. This format is supported only for the System.Half, System.Single, System.Double, and System.Numerics.BigInteger types.

In .NET Framework and in .NET Core versions earlier than 3.0, the "R" format specifier fails to successfully round-trip System.Double values in some cases. For both System.Double and System.Single values, the "R" format specifier offers relatively poor performance. Instead, we recommend that you use the "G17" format specifier for System.Double values and the "G9" format specifier to successfully round-trip System.Single values.

When a System.Numerics.BigInteger value is formatted using this specifier, its string representation contains all the significant digits in the System.Numerics.BigInteger value.

Although you can include a precision specifier, it is ignored. Round trips are given precedence over precision when using this specifier. The result string is affected by the formatting information of the current System.Globalization.NumberFormatInfo object. The following table lists the System.Globalization.NumberFormatInfo properties that control the formatting of the result string.

NumberFormatInfo property Description
System.Globalization.NumberFormatInfo.NegativeSign Defines the string that indicates that a number is negative.
System.Globalization.NumberFormatInfo.NumberDecimalSeparator Defines the string that separates integral digits from decimal digits.
System.Globalization.NumberFormatInfo.PositiveSign Defines the string that indicates that an exponent is positive.

The following example formats a System.Numerics.BigInteger value with the round-trip format specifier.

sample · csharpsamples/snippets/standard/base-types/format-strings/biginteger-r.csR format specifier with a BigInteger

csharp
using System;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      var value = BigInteger.Pow(Int64.MaxValue, 2);
      Console.WriteLine(value.ToString("R"));
   }
}
// The example displays the following output:
//      85070591730234615847396907784232501249

sample · vbsamples/snippets/standard/base-types/format-strings/biginteger-r.vbR format specifier with a BigInteger

vb
Imports System.Numerics

Module Example
    Public Sub Main()
        Dim value = BigInteger.Pow(Int64.MaxValue, 2)
        Console.WriteLine(value.ToString("R"))
    End Sub
End Module
' The example displays the following output:
'      85070591730234615847396907784232501249

To work around the problem of System.Double values formatted with the "R" standard numeric format string not successfully round-tripping if compiled using the /platform:x64 or /platform:anycpu switches and run on 64-bit systems, you can format System.Double values by using the "G17" standard numeric format string. The following example uses the "R" format string with a System.Double value that does not round-trip successfully, and also uses the "G17" format string to successfully round-trip the original value:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR_System/system.Double.ToString/cs/roundtripex1.csRoundTrip

csharp
// <Snippet5>
using System;
using System.Globalization;

public class Example
{
   static void Main(string[] args)
   {
      // <SnippetRoundTrip>
      Console.WriteLine("Attempting to round-trip a Double with 'R':");
      double initialValue = 0.6822871999174;
      string valueString = initialValue.ToString("R",
                                                 CultureInfo.InvariantCulture);
      double roundTripped = double.Parse(valueString,
                                         CultureInfo.InvariantCulture);
      Console.WriteLine($"{initialValue:R} = {roundTripped:R}: {initialValue.Equals(roundTripped)}\n");

      Console.WriteLine("Attempting to round-trip a Double with 'G17':");
      string valueString17 = initialValue.ToString("G17",
                                                   CultureInfo.InvariantCulture);
      double roundTripped17 = double.Parse(valueString17,
                                           CultureInfo.InvariantCulture);
      Console.WriteLine($"{initialValue:R} = {roundTripped17:R}: {initialValue.Equals(roundTripped17)}\n");
      // If compiled to an application that targets anycpu or x64 and run on an x64 system,
      // the example displays the following output:
      //       Attempting to round-trip a Double with 'R':
      //       .NET Framework:
      //       0.6822871999174 = 0.68228719991740006: False
      //       .NET:
      //       0.6822871999174 = 0.6822871999174: True
      //
      //       Attempting to round-trip a Double with 'G17':
      //       0.6822871999174 = 0.6822871999174: True
      // </SnippetRoundTrip>
   }
}
// </Snippet5>

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR_System/system.Double.ToString/vb/roundtripex1.vb5

vb
' Visual Basic .NET Document
Option Strict On

' <Snippet5>
Imports System.Globalization

Module Example
    Public Sub Main()
        Console.WriteLine("Attempting to round-trip a Double with 'R':")
        Dim initialValue As Double = 0.6822871999174
        Dim valueString As String = initialValue.ToString("R",
                                                 CultureInfo.InvariantCulture)
        Dim roundTripped As Double = Double.Parse(valueString,
                                                  CultureInfo.InvariantCulture)
        Console.WriteLine("{0:R} = {1:R}: {2}",
                          initialValue, roundTripped, initialValue.Equals(roundTripped))
        Console.WriteLine()

        Console.WriteLine("Attempting to round-trip a Double with 'G17':")
        Dim valueString17 As String = initialValue.ToString("G17",
                                                   CultureInfo.InvariantCulture)
        Dim roundTripped17 As Double = double.Parse(valueString17,
                                              CultureInfo.InvariantCulture)
        Console.WriteLine("{0:R} = {1:R}: {2}",
                          initialValue, roundTripped17, initialValue.Equals(roundTripped17))
    End Sub
End Module
' If compiled to an application that targets anycpu or x64 and run on an x64 system,
' the example displays the following output:
'       Attempting to round-trip a Double with 'R':
'       .NET Framework:
'       0.6822871999174 = 0.68228719991740006: False
'       .NET:
'       0.6822871999174 = 0.6822871999174: True
'
'       Attempting to round-trip a Double with 'G17':
'       0.6822871999174 = 0.6822871999174: True
' </Snippet5>

Hexadecimal format specifier (X)

The hexadecimal ("X") format specifier converts a number to a string of hexadecimal digits. The case of the format specifier indicates whether to use uppercase or lowercase characters for hexadecimal digits that are greater than 9. For example, use "X" to produce "ABCDEF", and "x" to produce "abcdef". This format is supported only for integral types.

The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.

For System.Numerics.BigInteger, positive values always have a leading zero to distinguish them from negative values. This ensures the output round-trips to the original value when parsed. For example, the number F converted with the format specifier "X1" is 0F because the hexadecimal number F represents the negative value -1.

The result string is not affected by the formatting information of the current System.Globalization.NumberFormatInfo object.

The following example formats System.Int32 values with the hexadecimal format specifier.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Formatting.Numeric.Standard/cs/Standard.cs9

csharp
using System;
using System.Globalization;

class formatting
{
   public static void Main()
   {
      Console.Clear();
      Console.WriteLine(CultureInfo.CurrentUICulture.Name);
      Console.WriteLine(CultureInfo.CurrentCulture.Name);
      Console.WriteLine();
      Console.WriteLine("Currency Format Specifier:");
      ShowCurrency();
      Console.WriteLine();
      Console.WriteLine("Decimal Format Specifier:");
      ShowDecimal();
      Console.WriteLine();
      Console.WriteLine("Exponentiation Format Specifier:");
      ShowExponentiation();
      Console.WriteLine();
      Console.WriteLine("Fixed Point Format Specifier:");
      ShowFixedPoint();
      Console.WriteLine();
      Console.WriteLine("'G' Format Specifier:");
      ShowGeneral();
      Console.WriteLine();
      Console.WriteLine("'N' Format Specifier:");
      ShowNumeric();
      Console.WriteLine();
      Console.WriteLine("Percent Format Specifier:");
      ShowPercent();
      Console.WriteLine();
      Console.WriteLine("Round-trip Format Specifier:");
      ShowRoundTrip();
      Console.WriteLine();
      Console.WriteLine("Hexadecimal Format Specifier:");
      ShowHex();
   }

   public static void ShowCurrency()
   {
      // <Snippet1>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));

      Console.WriteLine(value.ToString("C3",
                        CultureInfo.CreateSpecificCulture("da-DK")));
      // The example displays the following output on a system whose
      // current culture is English (United States):
      //       $12,345.68
      //       $12,345.679
      //       12.345,679 kr
      // </Snippet1>
   }

   public static void ShowDecimal()
   {
      // <Snippet2>
      int value;

      value = 12345;
      Console.WriteLine(value.ToString("D"));
      // Displays 12345
      Console.WriteLine(value.ToString("D8"));
      // Displays 00012345

      value = -12345;
      Console.WriteLine(value.ToString("D"));
      // Displays -12345
      Console.WriteLine(value.ToString("D8"));
      // Displays -00012345
      // </Snippet2>
   }

   public static void ShowExponentiation()
   {
      // <Snippet3>
      double value = 12345.6789;
      Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture));
      // Displays 1.234568E+004

      Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture));
      // Displays 1.2345678900E+004

      Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture));
      // Displays 1.2346e+004

      Console.WriteLine(value.ToString("E",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 1,234568E+004
      // </Snippet3>
   }

   public static void ShowFixedPoint()
   {
      // <Snippet4>
      int integerNumber;
      integerNumber = 17843;
      Console.WriteLine(integerNumber.ToString("F",
                        CultureInfo.InvariantCulture));
      // Displays 17843.00

      integerNumber = -29541;
      Console.WriteLine(integerNumber.ToString("F3",
                        CultureInfo.InvariantCulture));
      // Displays -29541.000

      double doubleNumber;
      doubleNumber = 18934.1879;
      Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture));
      // Displays 18934.19

      Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture));
      // Displays 18934

      doubleNumber = -1898300.1987;
      Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture));
      // Displays -1898300.2

      Console.WriteLine(doubleNumber.ToString("F3",
                        CultureInfo.CreateSpecificCulture("es-ES")));
      // Displays -1898300,199
      // </Snippet4>
   }

   public static void ShowGeneral()
   {
      // <Snippet5>
      double number;

      number = 12345.6789;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays  12345.6789
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 12345,6789

      Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture));
      // Displays 12345.68

      number = .0000023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 2.3E-06
      Console.WriteLine(number.ToString("G",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 2,3E-06

      number = .0023;
      Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
      // Displays 0.0023

      number = 1234;
      Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture));
      // Displays 1.2E+03

      number = Math.PI;
      Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture));
      // Displays 3.1416
      // </Snippet5>
   }

   public static void ShowNumeric()
   {
      // <Snippet6>
      double dblValue = -12445.6789;
      Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture));
      // Displays -12,445.68
      Console.WriteLine(dblValue.ToString("N1",
                        CultureInfo.CreateSpecificCulture("sv-SE")));
      // Displays -12 445,7

      int intValue = 123456789;
      Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture));
      // Displays 123,456,789.0
      // </Snippet6>
   }

   public static void ShowPercent()
   {
      // <Snippet7>
      double number = .2468013;
      Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture));
      // Displays 24.68 %
      Console.WriteLine(number.ToString("P",
                        CultureInfo.CreateSpecificCulture("hr-HR")));
      // Displays 24,68%
      Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture));
      // Displays 24.7 %
      // </Snippet7>
   }

   public static void ShowRoundTrip()
   {
      // <Snippet8>
      double value;

      value = Math.PI;
      Console.WriteLine(value.ToString("r"));
      // Displays 3.1415926535897931
      Console.WriteLine(value.ToString("r",
                        CultureInfo.CreateSpecificCulture("fr-FR")));
      // Displays 3,1415926535897931
      value = 1.623e-21;
      Console.WriteLine(value.ToString("r"));
      // Displays 1.623E-21
      // </Snippet8>
   }

   public static void ShowHex()
   {
      // <Snippet9>
      int value;

      value = 0x2045e;
      Console.WriteLine(value.ToString("x"));
      // Displays 2045e
      Console.WriteLine(value.ToString("X"));
      // Displays 2045E
      Console.WriteLine(value.ToString("X8"));
      // Displays 0002045E

      value = 123456789;
      Console.WriteLine(value.ToString("X"));
      // Displays 75BCD15
      Console.WriteLine(value.ToString("X2"));
      // Displays 75BCD15
      // </Snippet9>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Formatting.Numeric.Standard/vb/Standard.vb9

vb
' Visual Basic .NET Document
Option Strict On

Imports System.Globalization

Module modMain

    Public Sub Main()
        Console.Clear()
        Console.WriteLine(CultureInfo.CurrentUICulture.Name)
        Console.WriteLine(CultureInfo.CurrentCulture.Name)
        Console.WriteLine()
        Console.WriteLine("Currency Format Specifier:")
        ShowCurrency()
        Console.WriteLine()
        Console.WriteLine("Decimal Format Specifier:")
        ShowDecimal()
        Console.WriteLine()
        Console.WriteLine("Exponentiation Format Specifier:")
        ShowExponentiation()
        Console.WriteLine()
        Console.WriteLine("Fixed Point Format Specifier:")
        ShowFixedPoint()
        Console.WriteLine()
        Console.WriteLine("'G' Format Specifier:")
        ShowGeneral()
        Console.WriteLine()
        Console.WriteLine("'N' Format Specifier:")
        ShowNumeric()
        Console.WriteLine()
        Console.WriteLine("Percent Format Specifier:")
        ShowPercent()
        Console.WriteLine()
        Console.WriteLine("Round-trip Format Specifier:")
        ShowRoundTrip()
        Console.WriteLine()
        Console.WriteLine("Hexadecimal Format Specifier:")
        ShowHex()
        Console.WriteLine()
        Console.WriteLine("Invalid Format Specifier")
        ShowInvalid()
    End Sub

    Private Sub ShowCurrency()
        ' <Snippet1>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture))

        Console.WriteLine(value.ToString("C3", _
                          CultureInfo.CreateSpecificCulture("da-DK")))
        ' The example displays the following output on a system whose
        ' current culture is English (United States):
        '       $12,345.68
        '       $12,345.679
        '       kr 12.345,679
        ' </Snippet1> 
    End Sub

    Private Sub ShowDecimal()
        ' <Snippet2>
        Dim value As Integer

        value = 12345
        Console.WriteLine(value.ToString("D"))
        ' Displays 12345   
        Console.WriteLine(value.ToString("D8"))
        ' Displays 00012345

        value = -12345
        Console.WriteLine(value.ToString("D"))
        ' Displays -12345
        Console.WriteLine(value.ToString("D8"))
        ' Displays -00012345
        ' </Snippet2>
    End Sub

    Private Sub ShowExponentiation()
        ' <Snippet3>
        Dim value As Double = 12345.6789
        Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture))
        ' Displays 1.234568E+004

        Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture))
        ' Displays 1.2345678900E+004

        Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture))
        ' Displays 1.2346e+004

        Console.WriteLine(value.ToString("E", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 1,234568E+004
        ' </Snippet3>
    End Sub

    Private Sub ShowFixedPoint()
        ' <Snippet4>
        Dim integerNumber As Integer
        integerNumber = 17843
        Console.WriteLine(integerNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 17843.00

        integerNumber = -29541
        Console.WriteLine(integerNumber.ToString("F3", CultureInfo.InvariantCulture))
        ' Displays -29541.000

        Dim doubleNumber As Double
        doubleNumber = 18934.1879
        Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture))
        ' Displays 18934.19

        Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture))
        ' Displays 18934

        doubleNumber = -1898300.1987
        Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture))
        ' Displays -1898300.2

        Console.WriteLine(doubleNumber.ToString("F3", _
                          CultureInfo.CreateSpecificCulture("es-ES")))
        ' Displays -1898300,199                        
        ' </Snippet4>
    End Sub

    Private Sub ShowGeneral()
        ' <Snippet5>
        Dim number As Double

        number = 12345.6789
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays  12345.6789
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 12345,6789

        Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture))
        ' Displays 12345.68 

        number = .0000023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 2.3E-06       
        Console.WriteLine(number.ToString("G", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 2,3E-06

        number = .0023
        Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
        ' Displays 0.0023

        number = 1234
        Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture))
        ' Displays 1.2E+03

        number = Math.Pi
        Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture))
        ' Displays 3.1416    
        ' </Snippet5>
    End Sub

    Private Sub ShowNumeric()
        ' <Snippet6>
        Dim dblValue As Double = -12445.6789
        Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
        ' Displays -12,445.68
        Console.WriteLine(dblValue.ToString("N1", _
                          CultureInfo.CreateSpecificCulture("sv-SE")))
        ' Displays -12 445,7

        Dim intValue As Integer = 123456789
        Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture))
        ' Displays 123,456,789.0 
        ' </Snippet6>
    End Sub

    Private Sub ShowPercent()
        ' <Snippet7>
        Dim number As Double = .2468013
        Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture))
        ' Displays 24.68 %
        Console.WriteLine(number.ToString("P", _
                          CultureInfo.CreateSpecificCulture("hr-HR")))
        ' Displays 24,68%     
        Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture))
        ' Displays 24.7 %
        ' </Snippet7>
    End Sub

    Private Sub ShowRoundTrip()
        ' <Snippet8>
        Dim value As Double

        value = Math.Pi
        Console.WriteLine(value.ToString("r"))
        ' Displays 3.1415926535897931
        Console.WriteLine(value.ToString("r", _
                          CultureInfo.CreateSpecificCulture("fr-FR")))
        ' Displays 3,1415926535897931
        value = 1.623e-21
        Console.WriteLine(value.ToString("r"))
        ' Displays 1.623E-21
        ' </Snippet8>
    End Sub

    Private Sub ShowHex()
        ' <Snippet9>
        Dim value As Integer

        value = &h2045e
        Console.WriteLine(value.ToString("x"))
        ' Displays 2045e
        Console.WriteLine(value.ToString("X"))
        ' Displays 2045E
        Console.WriteLine(value.ToString("X8"))
        ' Displays 0002045E

        value = 123456789
        Console.WriteLine(value.ToString("X"))
        ' Displays 75BCD15
        Console.WriteLine(value.ToString("X2"))
        ' Displays 75BCD15
        ' </Snippet9>
    End Sub

    Private Sub ShowInvalid()
        Dim value As Integer = 12
        Dim specifier AS String = "Z"
        Try
            Console.WriteLine(value.ToString("Z"))
        Catch e As Exception
            Console.WriteLine("{0}: {1} is not a valid format specifier.", _
                              e.GetType().Name, specifier)
        End Try
    End Sub
End Module

Notes

This section contains additional information about using standard numeric format strings.

Control Panel settings

The settings in the Regional and Language Options item in Control Panel influence the result string produced by a formatting operation. Those settings are used to initialize the System.Globalization.NumberFormatInfo object associated with the current culture, which provides values used to govern formatting. Computers that use different settings generate different result strings.

In addition, if the System.Globalization.CultureInfo.%23ctor%28System.String%29 constructor is used to instantiate a new System.Globalization.CultureInfo object that represents the same culture as the current system culture, any customizations established by the Regional and Language Options item in Control Panel will be applied to the new System.Globalization.CultureInfo object. You can use the System.Globalization.CultureInfo.%23ctor%28System.String%2CSystem.Boolean%29 constructor to create a System.Globalization.CultureInfo object that does not reflect a system's customizations.

NumberFormatInfo properties

Formatting is influenced by the properties of the current System.Globalization.NumberFormatInfo object, which is provided implicitly by the current culture or explicitly by the System.IFormatProvider parameter of the method that invokes formatting. Specify a System.Globalization.NumberFormatInfo or System.Globalization.CultureInfo object for that parameter.

Integral and floating-point numeric types

Some descriptions of standard numeric format specifiers refer to integral or floating-point numeric types. The integral numeric types are System.Byte, System.SByte, System.Int16, System.Int32, System.Int64, System.UInt16, System.UInt32, System.UInt64, and System.Numerics.BigInteger. The floating-point numeric types are System.Decimal, System.Half, System.Single, and System.Double.

Floating-point infinities and NaN

Regardless of the format string, if the value of a System.Half, System.Single, or System.Double floating-point type is positive infinity, negative infinity, or not a number (NaN), the formatted string is the value of the respective System.Globalization.NumberFormatInfo.PositiveInfinitySymbol, System.Globalization.NumberFormatInfo.NegativeInfinitySymbol, or System.Globalization.NumberFormatInfo.NaNSymbol property that is specified by the currently applicable System.Globalization.NumberFormatInfo object.

Code example

The following example formats an integral and a floating-point numeric value using the en-US culture and all the standard numeric format specifiers. This example uses two particular numeric types (System.Double and System.Int32), but would yield similar results for any of the other numeric base types (System.Byte, System.SByte, System.Int16, System.Int32, System.Int64, System.UInt16, System.UInt32, System.UInt64, System.Numerics.BigInteger, System.Decimal, System.Half, and System.Single).

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR_System/system.X.ToString-and-Culture/cs/xts.csFinalExample

csharp
// This older code snippet replaced by a new one
// This code example demonstrates the ToString(String) and
// ToString(String, IFormatProvider) methods for integral and
// floating-point numbers, in conjunction with the standard
// numeric format specifiers.
// This code example uses the System.Int32 integral type and
// the System.Double floating-point type, but would yield
// similar results for any of the numeric types. The integral
// numeric types are System.Byte, SByte, Int16, Int32, Int64,
// UInt16, UInt32, and UInt64. The floating-point numeric types
// are Decimal, Single, and Double.
//
// using System;
// using System.Globalization;
// using System.Threading;
//
// class Sample
// {
//     public static void Main()
//     {
// // Format a negative integer or floating-point number in various ways.
//     int    integralVal = -12345;
//     double floatingVal = -1234.567d;
//
//     string msgCurrency =    "(C) Currency: . . . . . . ";
//     string msgDecimal  =    "(D) Decimal:. . . . . . . ";
//     string msgScientific =  "(E) Scientific: . . . . . ";
//     string msgFixedPoint =  "(F) Fixed point:. . . . . ";
//     string msgGeneral =     "(G) General (default):. . ";
//     string msgNumber =      "(N) Number: . . . . . . . ";
//     string msgPercent =     "(P) Percent:. . . . . . . ";
//     string msgRoundTrip =   "(R) Round-trip: . . . . . ";
//     string msgHexadecimal = "(X) Hexadecimal:. . . . . ";
//
//     string msg1 = "Use ToString(String) and the current thread culture.\n";
//     string msg2 = "Use ToString(String, IFormatProvider) and a specified culture.\n";
//     string msgCulture     = "Culture:";
//     string msgIntegralVal = "Integral value:";
//     string msgFloatingVal = "Floating-point value:";
//
//     CultureInfo ci;
// //
//     Console.Clear();
//     Console.WriteLine("Standard Numeric Format Specifiers:\n");
// // Display the values.
//     Console.WriteLine(msg1);
//
// // Display the thread current culture, which is used to format the values.
//     ci = Thread.CurrentThread.CurrentCulture;
//     Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName);
//
// // Display the integral and floating-point values.
//     Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal);
//     Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal);
//     Console.WriteLine();
//
// // Use the format specifiers that are only for integral types.
//     Console.WriteLine("Format specifiers only for integral types:");
//     Console.WriteLine(msgDecimal     + integralVal.ToString("D"));
//     Console.WriteLine(msgHexadecimal + integralVal.ToString("X"));
//     Console.WriteLine();
//
// // Use the format specifier that is only for the Single and Double
// // floating-point types.
//     Console.WriteLine("Format specifier only for the Single and Double types:");
//     Console.WriteLine(msgRoundTrip   + floatingVal.ToString("R"));
//     Console.WriteLine();
//
// // Use the format specifiers that are for integral or floating-point types.
//     Console.WriteLine("Format specifiers for integral or floating-point types:");
//     Console.WriteLine(msgCurrency    + floatingVal.ToString("C"));
//     Console.WriteLine(msgScientific  + floatingVal.ToString("E"));
//     Console.WriteLine(msgFixedPoint  + floatingVal.ToString("F"));
//     Console.WriteLine(msgGeneral     + floatingVal.ToString("G"));
//     Console.WriteLine(msgNumber      + floatingVal.ToString("N"));
//     Console.WriteLine(msgPercent     + floatingVal.ToString("P"));
//     Console.WriteLine();
//
// // Display the same values using a CultureInfo object. The CultureInfo class
// // implements IFormatProvider.
//     Console.WriteLine(msg2);
//
// // Display the culture used to format the values.
// // Create a European culture and change its currency symbol to "euro" because
// // this particular code example uses a thread current UI culture that cannot
// // display the euro symbol (€).
//     ci = new CultureInfo("de-DE");
//     ci.NumberFormat.CurrencySymbol = "euro";
//     Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName);
//
// // Display the integral and floating-point values.
//     Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal);
//     Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal);
//     Console.WriteLine();
//
// // Use the format specifiers that are only for integral types.
//     Console.WriteLine("Format specifiers only for integral types:");
//     Console.WriteLine(msgDecimal     + integralVal.ToString("D", ci));
//     Console.WriteLine(msgHexadecimal + integralVal.ToString("X", ci));
//     Console.WriteLine();
//
// // Use the format specifier that is only for the Single and Double
// // floating-point types.
//     Console.WriteLine("Format specifier only for the Single and Double types:");
//     Console.WriteLine(msgRoundTrip   + floatingVal.ToString("R", ci));
//     Console.WriteLine();
//
// // Use the format specifiers that are for integral or floating-point types.
//     Console.WriteLine("Format specifiers for integral or floating-point types:");
//     Console.WriteLine(msgCurrency    + floatingVal.ToString("C", ci));
//     Console.WriteLine(msgScientific  + floatingVal.ToString("E", ci));
//     Console.WriteLine(msgFixedPoint  + floatingVal.ToString("F", ci));
//     Console.WriteLine(msgGeneral     + floatingVal.ToString("G", ci));
//     Console.WriteLine(msgNumber      + floatingVal.ToString("N", ci));
//     Console.WriteLine(msgPercent     + floatingVal.ToString("P", ci));
//     Console.WriteLine();
//     }
// }
// /*
// This code example produces the following results:
//
// Standard Numeric Format Specifiers:
//
// Use ToString(String) and the current thread culture.
//
// Culture:                  English (United States)
// Integral value:           -12345
// Floating-point value:     -1234.567
//
// Format specifiers only for integral types:
// (D) Decimal:. . . . . . . -12345
// (X) Hexadecimal:. . . . . FFFFCFC7
//
// Format specifier only for the Single and Double types:
// (R) Round-trip: . . . . . -1234.567
//
// Format specifiers for integral or floating-point types:
// (C) Currency: . . . . . . ($1,234.57)
// (E) Scientific: . . . . . -1.234567E+003
// (F) Fixed point:. . . . . -1234.57
// (G) General (default):. . -1234.567
// (N) Number: . . . . . . . -1,234.57
// (P) Percent:. . . . . . . -123,456.70 %
//
// Use ToString(String, IFormatProvider) and a specified culture.
//
// Culture:                  German (Germany)
// Integral value:           -12345
// Floating-point value:     -1234.567
//
// Format specifiers only for integral types:
// (D) Decimal:. . . . . . . -12345
// (X) Hexadecimal:. . . . . FFFFCFC7
//
// Format specifier only for the Single and Double types:
// (R) Round-trip: . . . . . -1234,567
//
// Format specifiers for integral or floating-point types:
// (C) Currency: . . . . . . -1.234,57 euro
// (E) Scientific: . . . . . -1,234567E+003
// (F) Fixed point:. . . . . -1234,57
// (G) General (default):. . -1234,567
// (N) Number: . . . . . . . -1.234,57
// (P) Percent:. . . . . . . -123.456,70%
//
// */
//
// END OF OLD CODE SNIPPET

// <Snippet1>
using System;
using System.Globalization;
using System.Threading;

public class NumericFormats
{
   public static void Main()
   {
      // <SnippetFinalExample>
      // Display string representations of numbers for en-us culture
      CultureInfo ci = new CultureInfo("en-us");

      // Output floating point values
      double floating = 10761.937554;
      Console.WriteLine($"C: {floating.ToString("C", ci)}");           // Displays "C: $10,761.94"
      Console.WriteLine($"E: {floating.ToString("E03", ci)}");         // Displays "E: 1.076E+004"
      Console.WriteLine($"F: {floating.ToString("F04", ci)}");         // Displays "F: 10761.9376"
      Console.WriteLine($"G: {floating.ToString("G", ci)}");           // Displays "G: 10761.937554"
      Console.WriteLine($"N: {floating.ToString("N03", ci)}");         // Displays "N: 10,761.938"
      Console.WriteLine($"P: {(floating/10000).ToString("P02", ci)}"); // Displays "P: 107.62 %"
      Console.WriteLine($"R: {floating.ToString("R", ci)}");           // Displays "R: 10761.937554"
      Console.WriteLine();

      // Output integral values
      int integral = 8395;
      Console.WriteLine($"C: {integral.ToString("C", ci)}");           // Displays "C: $8,395.00"
      Console.WriteLine($"D: {integral.ToString("D6", ci)}");          // Displays "D: 008395"
      Console.WriteLine($"E: {integral.ToString("E03", ci)}");         // Displays "E: 8.395E+003"
      Console.WriteLine($"F: {integral.ToString("F01", ci)}");         // Displays "F: 8395.0"
      Console.WriteLine($"G: {integral.ToString("G", ci)}");           // Displays "G: 8395"
      Console.WriteLine($"N: {integral.ToString("N01", ci)}");         // Displays "N: 8,395.0"
      Console.WriteLine($"P: {(integral/10000.0).ToString("P02", ci)}"); // Displays "P: 83.95 %"
      Console.WriteLine($"X: 0x{integral.ToString("X", ci)}");           // Displays "X: 0x20CB"
      Console.WriteLine();
      // </SnippetFinalExample>
   }
}
// </Snippet1>

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR_System/system.X.ToString-and-Culture/vb/xts.vb1

vb
' This older code snippet replaced by a new one
' This code example demonstrates the ToString(String) and 
' ToString(String, IFormatProvider) methods for integral and
' floating-point numbers, in conjunction with the standard 
' numeric format specifiers.
' This code example uses the System.Int32 integral type and 
' the System.Double floating-point type, but would yield 
' similar results for any of the numeric types. The integral 
' numeric types are System.Byte, SByte, Int16, Int32, Int64, 
' UInt16, UInt32, and UInt64. The floating-point numeric types 
' are Decimal, Single, and Double.

' Imports System.Globalization
' Imports System.Threading

' 
' Class Sample
'     Public Shared Sub Main() 
'         ' Format a negative integer or floating-point number in various ways.
'         Dim integralVal As Integer = -12345
'         Dim floatingVal As Double  = -1234.567
'         
'         Dim msgCurrency As String =    "(C) Currency: . . . . . . "
'         Dim msgDecimal As String =     "(D) Decimal:. . . . . . . "
'         Dim msgScientific As String =  "(E) Scientific: . . . . . "
'         Dim msgFixedPoint As String =  "(F) Fixed point:. . . . . "
'         Dim msgGeneral As String =     "(G) General (default):. . "
'         Dim msgNumber As String =      "(N) Number: . . . . . . . "
'         Dim msgPercent As String =     "(P) Percent:. . . . . . . "
'         Dim msgRoundTrip As String =   "(R) Round-trip: . . . . . "
'         Dim msgHexadecimal As String = "(X) Hexadecimal:. . . . . "
'         
'         Dim msg1 As String = "Use ToString(String) and the current thread culture." + vbLf
'         Dim msg2 As String = "Use ToString(String, IFormatProvider) and a specified culture." + vbLf
'         Dim msgCulture As String = "Culture:"
'         Dim msgIntegralVal As String = "Integral value:"
'         Dim msgFloatingVal As String = "Floating-point value:"
'         
'         Dim ci As CultureInfo
'         '
'         Console.Clear()
'         Console.WriteLine("Standard Numeric Format Specifiers:" & vbLf)
'         ' Display the values.
'         Console.WriteLine(msg1)
'         
'         ' Display the thread current culture, which is used to format the values.
'         ci = Thread.CurrentThread.CurrentCulture
'         Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName)
'         
'         ' Display the integral and floating-point values.
'         Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal)
'         Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal)
'         Console.WriteLine()
'         
'         ' Use the format specifiers that are only for integral types.
'         Console.WriteLine("Format specifiers only for integral types:")
'         Console.WriteLine(msgDecimal     & integralVal.ToString("D"))
'         Console.WriteLine(msgHexadecimal & integralVal.ToString("X"))
'         Console.WriteLine()
'         
'         ' Use the format specifier that is only for the Single and Double 
'         ' floating-point types.
'         Console.WriteLine("Format specifier only for the Single and Double types:")
'         Console.WriteLine(msgRoundTrip   & floatingVal.ToString("R"))
'         Console.WriteLine()
'         
'         ' Use the format specifiers that are for integral or floating-point types.
'         Console.WriteLine("Format specifiers for integral or floating-point types:")
'         Console.WriteLine(msgCurrency    & floatingVal.ToString("C"))
'         Console.WriteLine(msgScientific  & floatingVal.ToString("E"))
'         Console.WriteLine(msgFixedPoint  & floatingVal.ToString("F"))
'         Console.WriteLine(msgGeneral     & floatingVal.ToString("G"))
'         Console.WriteLine(msgNumber      & floatingVal.ToString("N"))
'         Console.WriteLine(msgPercent     & floatingVal.ToString("P"))
'         Console.WriteLine()
'         
'         ' Display the same values using a CultureInfo object. The CultureInfo class 
'         ' implements IFormatProvider.
'         Console.WriteLine(msg2)
'         
'         ' Display the culture used to format the values. 
'         ' Create a European culture and change its currency symbol to "euro" because 
'         ' this particular code example uses a thread current UI culture that cannot 
'         ' display the euro symbol (€).
'         ci = New CultureInfo("de-DE")
'         ci.NumberFormat.CurrencySymbol = "euro"
'         Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName)
'         
'         ' Display the integral and floating-point values.
'         Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal)
'         Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal)
'         Console.WriteLine()
'         
'         ' Use the format specifiers that are only for integral types.
'         Console.WriteLine("Format specifiers only for integral types:")
'         Console.WriteLine(msgDecimal     & integralVal.ToString("D", ci))
'         Console.WriteLine(msgHexadecimal & integralVal.ToString("X", ci))
'         Console.WriteLine()
'         
'         ' Use the format specifier that is only for the Single and Double 
'         ' floating-point types.
'         Console.WriteLine("Format specifier only for the Single and Double types:")
'         Console.WriteLine(msgRoundTrip   & floatingVal.ToString("R", ci))
'         Console.WriteLine()
'         
'         ' Use the format specifiers that are for integral or floating-point types.
'         Console.WriteLine("Format specifiers for integral or floating-point types:")
'         Console.WriteLine(msgCurrency    & floatingVal.ToString("C", ci))
'         Console.WriteLine(msgScientific  & floatingVal.ToString("E", ci))
'         Console.WriteLine(msgFixedPoint  & floatingVal.ToString("F", ci))
'         Console.WriteLine(msgGeneral     & floatingVal.ToString("G", ci))
'         Console.WriteLine(msgNumber      & floatingVal.ToString("N", ci))
'         Console.WriteLine(msgPercent     & floatingVal.ToString("P", ci))
'         Console.WriteLine()
'     End Sub
' End Class
'
'This code example produces the following results:
'
'Standard Numeric Format Specifiers:
'
'Use ToString(String) and the current thread culture.
'
'Culture:                  English (United States)
'Integral value:           -12345
'Floating-point value:     -1234.567
'
'Format specifiers only for integral types:
'(D) Decimal:. . . . . . . -12345
'(X) Hexadecimal:. . . . . FFFFCFC7
'
'Format specifier only for the Single and Double types:
'(R) Round-trip: . . . . . -1234.567
'
'Format specifiers for integral or floating-point types:
'(C) Currency: . . . . . . ($1,234.57)
'(E) Scientific: . . . . . -1.234567E+003
'(F) Fixed point:. . . . . -1234.57
'(G) General (default):. . -1234.567
'(N) Number: . . . . . . . -1,234.57
'(P) Percent:. . . . . . . -123,456.70 %
'
'Use ToString(String, IFormatProvider) and a specified culture.
'
'Culture:                  German (Germany)
'Integral value:           -12345
'Floating-point value:     -1234.567
'
'Format specifiers only for integral types:
'(D) Decimal:. . . . . . . -12345
'(X) Hexadecimal:. . . . . FFFFCFC7
'
'Format specifier only for the Single and Double types:
'(R) Round-trip: . . . . . -1234,567
'
'Format specifiers for integral or floating-point types:
'(C) Currency: . . . . . . -1.234,57 euro
'(E) Scientific: . . . . . -1,234567E+003
'(F) Fixed point:. . . . . -1234,57
'(G) General (default):. . -1234,567
'(N) Number: . . . . . . . -1.234,57
'(P) Percent:. . . . . . . -123.456,70%
'
' END OF OLD CODE SNIPPET

' Visual Basic .NET Document
' <Snippet1>
Option Strict On

Imports System.Globalization
Imports System.Threading

Module NumericFormats
    Public Sub Main()
        ' Display string representations of numbers for en-us culture
        Dim ci As New CultureInfo("en-us")

        ' Output floating point values
        Dim floating As Double = 10761.937554
        Console.WriteLine("C: {0}", _
                floating.ToString("C", ci))           ' Displays "C: $10,761.94"
        Console.WriteLine("E: {0}", _
                floating.ToString("E03", ci))         ' Displays "E: 1.076E+004"
        Console.WriteLine("F: {0}", _
                floating.ToString("F04", ci))         ' Displays "F: 10761.9376"         
        Console.WriteLine("G: {0}", _
                floating.ToString("G", ci))           ' Displays "G: 10761.937554"
        Console.WriteLine("N: {0}", _
                floating.ToString("N03", ci))         ' Displays "N: 10,761.938"
        Console.WriteLine("P: {0}", _
                (floating / 10000).ToString("P02", ci)) ' Displays "P: 107.62 %"
        Console.WriteLine("R: {0}", _
                floating.ToString("R", ci))           ' Displays "R: 10761.937554"            
        Console.WriteLine()

        ' Output integral values
        Dim integral As Integer = 8395
        Console.WriteLine("C: {0}", _
                integral.ToString("C", ci))           ' Displays "C: $8,395.00"
        Console.WriteLine("D: {0}", _
                integral.ToString("D6"))              ' Displays "D: 008395" 
        Console.WriteLine("E: {0}", _
                integral.ToString("E03", ci))         ' Displays "E: 8.395E+003"
        Console.WriteLine("F: {0}", _
                integral.ToString("F01", ci))         ' Displays "F: 8395.0"    
        Console.WriteLine("G: {0}", _
                integral.ToString("G", ci))           ' Displays "G: 8395"
        Console.WriteLine("N: {0}", _
                integral.ToString("N01", ci))         ' Displays "N: 8,395.0"
        Console.WriteLine("P: {0}", _
                (integral / 10000).ToString("P02", ci)) ' Displays "P: 83.95 %"
        Console.WriteLine("X: 0x{0}", _
                integral.ToString("X", ci))           ' Displays "X: 0x20CB"
        Console.WriteLine()
    End Sub
End Module
' </Snippet1>

See also