C# Int32 ToString(String, IFormatProvider)

Description

Int32 ToString(String, IFormatProvider) converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.

Syntax

Int32.ToString(String, IFormatProvider) has the following syntax.


public string ToString(
  string format,
  IFormatProvider provider
)

Parameters

Int32.ToString(String, IFormatProvider) has the following parameters.

  • format - A standard or custom numeric format string.
  • provider - An object that supplies culture-specific formatting information.

Returns

Int32.ToString(String, IFormatProvider) method returns The string representation of the value of this instance as specified by format and provider.

Example

The following example displays a positive and a negative value using each of the supported standard numeric format specifiers for three different cultures.


using System.Globalization;
using System;//w  w w.j  a  va2 s.c  o  m
public class MainClass{
  public static void Main(String[] argv){  
    // Define cultures whose formatting conventions are to be used.
    CultureInfo[] cultures = {CultureInfo.CreateSpecificCulture("en-US"), 
                              CultureInfo.CreateSpecificCulture("fr-FR"), 
                              CultureInfo.CreateSpecificCulture("es-ES") };
    int positiveNumber = 1679;
    int negativeNumber = -3045;
    string[] specifiers = {"G", "C", "D8", "E2", "F", "N", "P", "X8"}; 
    
    foreach (string specifier in specifiers)
    {
       foreach (CultureInfo culture in cultures)
       {
          // Display values with "G" format specifier.
          Console.WriteLine("{0} format using {1} culture: {2, 16} {3, 16}",  
                            specifier, culture.Name, 
                            positiveNumber.ToString(specifier, culture), 
                            negativeNumber.ToString(specifier, culture));
       }
       Console.WriteLine();
    }
  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version