Csharp/CSharp Tutorial/Data Type/int format

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Formatting integers

<source lang="csharp">class MainClass {

 public static void Main(){
   
   int myInt = 12345;
   int myInt2 = 67890;
   System.Console.WriteLine("myInt = {0, 6}, myInt2 = {1, 5}", myInt, myInt2);
   System.Console.WriteLine("myInt using 10:d = {0, 10:d}", myInt);
   System.Console.WriteLine("myInt using 10:x = {0, 10:x2}", myInt);
 }

}</source>

myInt =  12345, myInt2 = 67890
myInt using 10:d =      12345
myInt using 10:x =       3039

int format: {0:C}, {0:D}, {0:E}, {0:F}, {0:G}, {0:N}, {0:P}, {0:X}

<source lang="csharp">using System; public class FormatSpecApp {

   public static void Main(string[] args)
   {
       int i = 123456;
       Console.WriteLine("{0:C}", i); // ?23,456.00
       Console.WriteLine("{0:D}", i); // 123456
       Console.WriteLine("{0:E}", i); // 1.234560E+005
       Console.WriteLine("{0:F}", i); // 123456.00
       Console.WriteLine("{0:G}", i); // 123456
       Console.WriteLine("{0:N}", i); // 123,456.00
       Console.WriteLine("{0:P}", i); // 12,345,600.00 %
       Console.WriteLine("{0:X}", i); // 1E240
   }

}</source>