Partilhar via


Standard Numeric Format Strings Output Examples 

The following tables contain a few examples of output produced by using the standard numeric format strings.

Each column in the following tables corresponds to a row in the Standard Numeric Format Strings topic. The output was produced by the ToString method in conjunction with the standard numeric format specifiers and specific data types, values, and cultures.

Currency

Format string Culture Data type Value Output

C

en-US

Double

12345.6789

$12,345.68

C

de-DE

Double

12345.678

12.345,68 €

Decimal

Format string Culture Data type Value Output

D

en-US

Int32

12345

12345

D8

en-US

Int32

12345

00012345

Scientific (Exponential)

Format string Culture Data type Value Output

E

en-US

Double

12345.6789

1.234568E+004

E10

en-US

Double

12345.6789

1.2345678900E+004

E

fr-FR

Double

12345.6789

1,234568E+004

e4

en-US

Double

12345.6789

1.2346e+004

Fixed-Point

Format string Culture Data type Value Output

F

en-US

Double

12345.6789

12345.68

F

es-ES

Double

12345.6789

12345,68

F0

en-US

Double

12345.6789

123456

F6

en-US

Double

12345.6789

12345.678900

General

Format string Culture Data type Value Output

G

en-US

Double

12345.6789

12345.6789

G7

en-US

Double

12345.6789

12345.68

G

en-US

Double

0.0000023

2.3E-6

G

en-US

Double

0.0023

0.0023

G2

en-US

Double

1234

1.2E3

G

en-US

Double

Math.PI

3.14159265358979

Number

Format string Culture Data type Value Output

N

en-US

Double

12345.6789

12,345.68

N

sv-SE

Double

12345.6789

12 345,68

N4

en-US

Double

123456789

123,456,789.0000

Percent

Format string Culture Data type Value Output

P

en-US

Double

.126

12.60 %

Round-Trip

Format string Culture Data type Value Output

r

en-US

Double

Math.PI

3.141592653589793

Hexadecimal

Format string Culture Data type Value Output

x

en-US

Int32

0x2c45e

2c45e

X

en-US

Int32

0x2c45e

2C45E

X8

en-US

Int32

0x2c45e

0002C45E

x

en-US

Int32

123456789

75bcd15

Example

The following code example formats an integral and a floating-point numeric value using the thread current culture, a specified culture, and all the standard numeric formats. This code example uses two particular numeric types, but would yield similar results for any of the numeric base types (Byte, SByte, Int16, Int32, Int64, UInt16, UInt64, Decimal, Single, and Double).

' 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
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 'Main
End Class 'Sample
'
'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%
'
// 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%

*/

See Also

Concepts

Standard Numeric Format Strings
Custom Numeric Format Strings