Int64.ToString Method (String, IFormatProvider)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function ToString ( _
    format As String, _
    provider As IFormatProvider _
) As String
[SecuritySafeCriticalAttribute]
public string ToString(
    string format,
    IFormatProvider provider
)

Parameters

  • format
    Type: System.String
    A standard or custom numeric format string (see Remarks).

Return Value

Type: System.String
The string representation of the value of this instance as specified by format and provider.

Implements

IFormattable.ToString(String, IFormatProvider)

Remarks

If format is nulla null reference (Nothing in Visual Basic) or an empty string (""), the return value for this instance is formatted with the general numeric format specifier ("G").

The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

The provider parameter is an IFormatProvider that obtains a NumberFormatInfo object. The NumberFormatInfo provides culture-specific format information about this instance. If provider is nulla null reference (Nothing in Visual Basic), the return value for this instance is formatted with the NumberFormatInfo for the current culture.

Examples

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 format specifiers. 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, UInt32, UInt64, Decimal, Single, and Double).

Option Strict On

Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' 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
      outputBlock.Text += String.Format("C: {0}", _
              floating.ToString("C", ci))           ' Displays "C: $10,761.94" & vbCrLf
      outputBlock.Text += String.Format("E: {0}", _
              floating.ToString("E03", ci))         ' Displays "E: 1.076E+004" & vbCrLf
      outputBlock.Text += String.Format("F: {0}", _
              floating.ToString("F04", ci))         ' Displays "F: 10761.9376"          & vbCrLf
      outputBlock.Text += String.Format("G: {0}", _
              floating.ToString("G", ci))           ' Displays "G: 10761.937554" & vbCrLf
      outputBlock.Text += String.Format("N: {0}", _
              floating.ToString("N03", ci))         ' Displays "N: 10,761.938" & vbCrLf
      outputBlock.Text += String.Format("P: {0}", _
              (floating / 10000).ToString("P02", ci)) ' Displays "P: 107.62 %" & vbCrLf
      outputBlock.Text += String.Format("R: {0}", _
              floating.ToString("R", ci))           ' Displays "R: 10761.937554"             & vbCrLf
      outputBlock.Text += vbCrLf

      ' Output integral values
      Dim integral As Integer = 8395
      outputBlock.Text += String.Format("C: {0}", _
              integral.ToString("C", ci))           ' Displays "C: $8,395.00" & vbCrLf
      outputBlock.Text += String.Format("D: {0}", _
              integral.ToString("D6"))              ' Displays D: 008395""  & vbCrLf
      outputBlock.Text += String.Format("E: {0}", _
              integral.ToString("E03", ci))         ' Displays "E: 8.395E+003" & vbCrLf
      outputBlock.Text += String.Format("F: {0}", _
              integral.ToString("F01", ci))         ' Displays "F: 8395.0"     & vbCrLf
      outputBlock.Text += String.Format("G: {0}", _
              integral.ToString("G", ci))           ' Displays "G: 8395" & vbCrLf
      outputBlock.Text += String.Format("N: {0}", _
              integral.ToString("N01", ci))         ' Displays "N: 8,395.0" & vbCrLf
      outputBlock.Text += String.Format("P: {0}", _
              (integral / 10000).ToString("P02", ci)) ' Displays "P: 83.95 %" & vbCrLf
      outputBlock.Text += String.Format("X: 0x{0}", _
              integral.ToString("X", ci))           ' Displays "X: 0x20CB" & vbCrLf
      outputBlock.Text += vbCrLf
   End Sub
End Module
using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Display string representations of numbers for en-us culture
      CultureInfo ci = new CultureInfo("en-us");

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

      // Output integral values
      int integral = 8395;
      outputBlock.Text += String.Format("C: {0}",
              integral.ToString("C", ci)) + "\n";           // Displays "C: $8,395.00"
      outputBlock.Text += String.Format("D: {0}",
              integral.ToString("D6", ci)) + "\n";          // Displays D: 008395"" 
      outputBlock.Text += String.Format("E: {0}",
              integral.ToString("E03", ci)) + "\n";         // Displays "E: 8.395E+003"
      outputBlock.Text += String.Format("F: {0}",
              integral.ToString("F01", ci)) + "\n";         // Displays "F: 8395.0"    
      outputBlock.Text += String.Format("G: {0}",
              integral.ToString("G", ci)) + "\n";           // Displays "G: 8395"
      outputBlock.Text += String.Format("N: {0}",
              integral.ToString("N01", ci)) + "\n";         // Displays "N: 8,395.0"
      outputBlock.Text += String.Format("P: {0}",
              (integral / 10000).ToString("P02", ci)) + "\n"; // Displays "P: 83.95 %"
      outputBlock.Text += String.Format("X: 0x{0}",
              integral.ToString("X", ci)) + "\n";           // Displays "X: 0x20CB"
      outputBlock.Text += "\n";
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.