在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 前言
int x = 16;
decimal y = 3.57m; string h = String.Format( "item {0} sells at {1:C}", x, y ); Console.WriteLine(h);
item 16 sells at ¥3.57
Console.WriteLine( "Hello {0} {1} {2} {3} {4} {5} {6} {7} {8}", 123, 45.67, true, 'Q', 4, 5, 6, 7, '8');
string u = String.Format("Hello {0} {1} {2} {3} {4} {5} {6} {7} {8}", 123, 45.67, true, 'Q', 4, 5, 6, 7, '8'); Console.WriteLine(u);
Hello 123 45.67 True Q 4 5 6 7 8
Hello 123 45.67 True Q 4 5 6 7 8
public class TestConsoleApp
{ public static void Main(string[] args) { Console.WriteLine(123); Console.WriteLine("{0}", 123); Console.WriteLine("{0:D3}", 123); } }
123
123 123
string s = string.Format("123");
string t = string.Format("{0}", 123); string u = string.Format("{0:D3}", 123); Console.WriteLine(s); Console.WriteLine(t); Console.WriteLine(u);
Console.WriteLine("{0,5} {1,5}", 123, 456); // 右对齐
Console.WriteLine("{0,-5} {1,-5}", 123, 456); // 左对齐
123 456
123 456
Console.WriteLine("{0,-10:D6} {1,-10:D6}", 123, 456);
000123 000456
Console.WriteLine("\n{0,-10}{1,-3}", "Name","Salary");
Console.WriteLine("----------------"); Console.WriteLine("{0,-10}{1,6}", "Bill", 123456); Console.WriteLine("{0,-10}{1,6}", "Polly", 7890);
Name Salary
---------------- Bill 123456 Polly 7890
如果我们使用下面的表达方式,让我们看看会发生什么
public class FormatSpecApp
{ public static void Main(string[] args) { int i = 123456; Console.WriteLine("{0:C}", i); // ¥123,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 } }
Console.WriteLine("{0:C5}", i); // ¥123,456.00
Console.WriteLine("{0:D5}", i); // 123456 Console.WriteLine("{0:E5}", i); // 1.23456E+005 Console.WriteLine("{0:F5}", i); // 123456.00000 Console.WriteLine("{0:G5}", i); // 1.23456E5 Console.WriteLine("{0:N5}", i); // 123,456.00000 Console.WriteLine("{0:P5}", i); // 12,345,600.00000 % Console.WriteLine("{0:X5}", i); // 1E240
double d = 1.2345678901234567890;
Console.WriteLine("Floating-Point:\t{0:F16}", d); // 1.2345678901234600 Console.WriteLine("Roundtrip:\t{0:R16}", d); // 1.2345678901234567
看下面的例子:
double i = 123456.42;
Console.WriteLine(); Console.WriteLine("{0:000000.00}", i); //123456.42 Console.WriteLine("{0:00.00000000e+0}", i); //12.34564200e+4 Console.WriteLine("{0:0,.}", i); //123 Console.WriteLine("{0:#0.000}", i); // 123456.420 Console.WriteLine("{0:#0.000;(#0.000)}", i); // 123456.420 Console.WriteLine("{0:#0.000;(#0.000);<zero>}", i); // 123456.420 Console.WriteLine("{0:#%}", i); // 12345642% i = -123456.42; Console.WriteLine(); Console.WriteLine("{0:000000.00}", i); //-123456.42 Console.WriteLine("{0:00.00000000e+0}", i); //-12.34564200e+4 Console.WriteLine("{0:0,.}", i); //-123 Console.WriteLine("{0:#0.000}", i); // -123456.420 Console.WriteLine("{0:#0.000;(#0.000)}", i); // (123456.420) Console.WriteLine("{0:#0;(#0);<zero>}", i); // (123456) Console.WriteLine("{0:#%}", i); // -12345642% i = 0; Console.WriteLine(); Console.WriteLine("{0:0,.}", i); //0 Console.WriteLine("{0:#0}", i); // 0 Console.WriteLine("{0:#0;(#0)}", i); // 0 Console.WriteLine("{0:#0;(#0);<zero>}", i); // <zero> Console.WriteLine("{0:#%}", i); // %
public class NumParsingApp
{ public static void Main(string[] args) { int i = int.Parse("12345"); Console.WriteLine("i = {0}", i); int j = Int32.Parse("12345"); Console.WriteLine("j = {0}", j); double d = Double.Parse("1.2345E+6"); Console.WriteLine("d = {0:F}", d); string s = i.ToString(); Console.WriteLine("s = {0}", s); } }
i = 12345
j = 12345 d = 1234500.00 s = 12345
string t = " -1,234,567.890 ";
//double g = double.Parse(t); // 和下面的代码干同样的事情 double g = double.Parse(t, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite); Console.WriteLine("g = {0:F}", g);
g = -1234567.89
string u = "¥ -1,234,567.890 ";
NumberFormatInfo ni = new NumberFormatInfo(); ni.CurrencySymbol = "¥"; double h = Double.Parse(u, NumberStyles.Any, ni); Console.WriteLine("h = {0:F}", h);
h = -1234567.89
int k = 12345;
CultureInfo us = new CultureInfo("en-US"); string v = k.ToString("c", us); Console.WriteLine(v);
$12,345.00
CultureInfo dk = new CultureInfo("da-DK");
string w = k.ToString("c", dk); Console.WriteLine(w);
kr 12.345,00
using System.Globalization;
public class DatesApp { public static void Main(string[] args) { DateTime dt = DateTime.Now; Console.WriteLine(dt); Console.WriteLine("date = {0}, time = {1}\n", dt.Date, dt.TimeOfDay); } }
23/06/2001 17:55:10
date = 23/06/2001 00:00:00, time = 17:55:10.3839296
DateTimeFormatInfo.InvariantInfo属性得到了默认的只读的DateTimeFormatInfo实例,它与文化无关。你可以创建自定义的模式。要注意到的是InvariantInfo不一定和本地的格式一样。Invariant等于美国格式。另外,如果你向 DateTime.Format方法传递的第二个参数是null,DateTimeFormatInfo将会是默认的CurrentInfo。比如
Console.WriteLine(dt.ToString("d", dtfi));
Console.WriteLine(dt.ToString("d", null)); Console.WriteLine();
06/23/2001
23/06/2001
DateTimeFormatInfo dtfi; Console.Write("[I]nvariant or [C]urrent Info?: "); if (Console.Read() |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论