Printer Friendly Version      Send     
Click to Rate and Give Feedback
MSDN
MSDN Library
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Developer's Guide
Standard Numeric Format Strings Output Examples
[Note: This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

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

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).

Visual Basic
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

C#
using System;
using System.Globalization;
using System.Threading;

public class NumericFormats
{
   public static void Main()
   {
      // 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: {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
      int integral = 8395;
      Console.WriteLine("C: {0}", 
              integral.ToString("C", ci));           // Displays "C: $8,395.00"
      Console.WriteLine("D: {0}", 
              integral.ToString("D6", ci));          // 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();
   }
}

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker