ToString Method (String, IFormatProvider)
Collapse the table of content
Expand the table of content

Int64.ToString Method (String, IFormatProvider)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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)

public string ToString(
	string format,
	IFormatProvider provider
)

Parameters

format
Type: System.String
A standard or custom numeric format string (see Remarks).
provider
Type: System.IFormatProvider
An IFormatProvider that supplies culture-specific formatting information about this instance.

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)

If format is null 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 null, the return value for this instance is formatted with the NumberFormatInfo for the current culture.

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


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";
   }
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft