// // .
Using System;
class FormatDemo {
static void Main() { double v = 17688.65849; double v2 = 0.15; int x = 21;
Console.WriteLine("{0:F2}", v);
Console.WriteLine("{0:N5}", v);
Console.WriteLine ("{0:e}", v);
Console.WriteLine("{0:r}", v);
Console.WriteLine("{0:p}", v2);
Console.WriteLine("{0:X}", x);
Console.WriteLine("{0:D12}", x);
Console.WriteLine("{0:C}", 189.99);
}
}
.
17688.66
17.688.65849 1.768866+004
17688.65849 15.00 %
$189.99
.
, , , , . , WriteLine(). , , . .
Using System;
class FormatDemo2 { static void Main() {
// .
Console.WriteLine("{0:F2} {0:F3} {0:}", 10.12345);
// .
Console.WriteLine("{2:d} {0:d} {l:d}", 1, 2, 3);
}
}
.
10.12 10.123 1.012345+001 3 12
WriteLine () 10.12345 . , . WriteLine () . , - , , . .
String. Format () ToString ()
WriteLine (), , , . , . , Windows, - , -.
, . String. Format (), ToStringO, C# . .
|
|
String. Format ()
Format (), String, . . 22.5. Format () WriteLine (), , , .
Public
Static string
argO
Format
(string format,
,
Obj ect
argO)
format. format,
Public
Static string
argO
Format
(string format,
,
Object
argO, object argl)
format, argl . format,
Public
Static string
argO, argl 2
Format
(string format,
,
Obj ect
argO, object argl,
format. format,
Obj ect
arg2)
Public
Static string
, args,
Format
(string format,
,
Params
object[] args)
format. format,
Public
Static string
, args,
Format
(IFormatProvider
,
provider, string format,
format,
Params
object[] args)
provider. format,
, String. Format (). , .
// String.Format() , using System;
class FormatDemo { static void Main() {
double v = 17688.65849; double v2 = 0.15; int x = 21;
string str = String.Format("{0:F2}", v);
Console.WriteLine(str);
|
|
str = String.Format("{0:N5}", v); Console.WriteLine(str);
str = String.Format("{0:e}", v); Console.WriteLine(str);
str = String.Format("{0:r}", v); Console.WriteLine (str);
str = String.Format("{0:p}", v2); Console.WriteLine(str);
str = String.Format("{0:X}", x); Console.WriteLine(str);
str = String.Format("{0:D12}", x); Console.WriteLine(str);
str = String.Format("{0:C}", 189.99); Console.WriteLine (str);
}
}
WriteLine (), String. Format () , . , 1 10.
// Format().
Using System;
class FormatDemo2 { static void Main() { int i;
int sum = 0; int prod = 1; string str;
/* 1 10. */ for(i=l; i <= 10; i++) {
sum += i; prod *= i;
str = String.Format(":{0,3:D} :{1,8:D}", sum, prod);
Console.WriteLine(str);
}
: 45 : 362880
: 55 : 3628800