C# Int32 ToString(String)

Description

Int32 ToString(String) converts the numeric value of this instance to its equivalent string representation, using the specified format.

Syntax

Int32.ToString(String) has the following syntax.


public string ToString(
  string format
)

Parameters

Int32.ToString(String) has the following parameters.

  • format - A standard or custom numeric format string.

Returns

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

Example

The following example displays an Int32 value using each of the supported standard numeric format specifiers, together with two custom numeric format strings.


/* w  w  w . j  a va  2 s  .  com*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    int value = -12345678;
    string specifier;
    
    // Use standard numeric format specifier.
    specifier = "G";
    Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
    
    specifier = "C";
    Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
    
    specifier = "D8";
    Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
    
    specifier = "E4";
    Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
    
    specifier = "e3";
    Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
    
    specifier = "F";
    Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
    
    specifier = "N";
    Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
    
    specifier = "P";
    Console.WriteLine("{0}: {1}", specifier, (value/100000).ToString(specifier));
    
    specifier = "X";
    Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
    
    
    // Use custom numeric format specifiers.
    specifier = "0,0.000";
    Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
    
    specifier = "#,#.00#;(#,#.00#)";
    Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier));

  }
}

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