c# ToString 格式化数字

 

 

 
double[] numbers= {1054.32179, -195489100.8377, 1.0437E21, -1.0573e-05}; 
string[] specifiers = { "C", "E", "e", "F", "G", "N", "P", "R", "#,000.000", "0.###E-000"}; 
foreach (double number in numbers) { 
Console.WriteLine("Formatting of {0}:", number); 
foreach (string specifier in specifiers) 
Console.WriteLine(" {0,5}: {1}", specifier, number.ToString(specifier)); 
Console.WriteLine(); } 
// The example displays the following output to the console: // Formatting of 1054.32179: // C: $1,054.32 // E: 1.054322E+003 // e: 1.054322e+003 // F: 1054.32 // G: 1054.32179 // N: 1,054.32 // P: 105,432.18 % // R: 1054.32179 // #,000.000: 1,054.322 // 0.###E-000: 1.054E003 //  // Formatting of -195489100.8377: // C: ($195,489,100.84) // E: -1.954891E+008 // e: -1.954891e+008 // F: -195489100.84 // G: -195489100.8377 // N: -195,489,100.84 // P: -19,548,910,083.77 % // R: -195489100.8377 // #,000.000: -195,489,100.838 // 0.###E-000: -1.955E008 //  // Formatting of 1.0437E+21: // C: $1,043,700,000,000,000,000,000.00 // E: 1.043700E+021 // e: 1.043700e+021 // F: 1043700000000000000000.00 // G: 1.0437E+21 // N: 1,043,700,000,000,000,000,000.00 // P: 104,370,000,000,000,000,000,000.00 % // R: 1.0437E+21 // #,000.000: 1,043,700,000,000,000,000,000.000 // 0.###E-000: 1.044E021 //  // Formatting of -1.0573E-05: // C: $0.00 // E: -1.057300E-005 // e: -1.057300e-005 // F: 0.00 // G: -1.0573E-05 // N: 0.00 // P: 0.00 % // R: -1.0573E-05 // #,000.000: 000.000 // 0.###E-000: -1.057E-005   
  1.  
  2.   
  3.     //2007-4-24   
  4.   
  5.     this.TextBox7.Text = System.DateTime.Now.ToString("d");   
  6.   
  7.   
  8.   
  9.     //2007年4月24日 16:30:15   
  10.   
  11.     this.TextBox8.Text = System.DateTime.Now.ToString("F");   
  12.   
  13.     //2007年4月24日 16:30   
  14.   
  15.     this.TextBox9.Text = System.DateTime.Now.ToString("f");   
  16.   
  17.   
  18.   
  19.     //2007-4-24 16:30:15   
  20.   
  21.     this.TextBox10.Text = System.DateTime.Now.ToString("G");   
  22.   
  23.     //2007-4-24 16:30   
  24.   
  25.     this.TextBox11.Text = System.DateTime.Now.ToString("g");   
  26.   
  27.   
  28.   
  29.     //16:30:15   
  30.   
  31.     this.TextBox12.Text = System.DateTime.Now.ToString("T");   
  32.   
  33.     //16:30   
  34.   
  35.     this.TextBox13.Text = System.DateTime.Now.ToString("t");   
  36.   
  37.   
  38.   
  39.     //2007年4月24日 8:30:15   
  40.   
  41.     this.TextBox14.Text = System.DateTime.Now.ToString("U");   
  42.   
  43.     //2007-04-24 16:30:15Z   
  44.   
  45.     this.TextBox15.Text = System.DateTime.Now.ToString("u");   
  46.   
  47.   
  48.   
  49.     //4月24日   
  50.   
  51.     this.TextBox16.Text = System.DateTime.Now.ToString("m");   
  52.   
  53.     this.TextBox16.Text = System.DateTime.Now.ToString("M");   
  54.   
  55.     //Tue, 24 Apr 2007 16:30:15 GMT   
  56.   
  57.     this.TextBox17.Text = System.DateTime.Now.ToString("r");   
  58.   
  59.     this.TextBox17.Text = System.DateTime.Now.ToString("R");   
  60.   
  61.     //2007年4月   
  62.   
  63.     this.TextBox19.Text = System.DateTime.Now.ToString("y");   
  64.   
  65.     this.TextBox19.Text = System.DateTime.Now.ToString("Y");   
  66.   
  67.     //2007-04-24T15:52:19.1562500+08:00   
  68.   
  69.     this.TextBox20.Text = System.DateTime.Now.ToString("o");   
  70.   
  71.     this.TextBox20.Text = System.DateTime.Now.ToString("O");   
  72.   
  73.     //2007-04-24T16:30:15   
  74.   
  75.     this.TextBox18.Text = System.DateTime.Now.ToString("s");   
  76.   
  77.     //2007-04-24 15:52:19   
  78.   
  79.     this.TextBox21.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");   
  80.   
  81.     //2007年04月24 15时56分48秒   
  82.   
  83.     this.TextBox22.Text = System.DateTime.Now.ToString("yyyy年MM月dd HH时mm分ss秒");   
  84.   
  85.   
  86.   
  87.   
  88.   
  89.     //星期二, 四月 24 2007   
  90.   
  91.     this.TextBox1.Text = System.DateTime.Now.ToString("dddd, MMMM dd yyyy");   
  92.   
  93.     //二, 四月 24 '07   
  94.   
  95.     this.TextBox2.Text = System.DateTime.Now.ToString("ddd, MMM d \"'\"yy");   
  96.   
  97.     //星期二, 四月 24   
  98.   
  99.     this.TextBox3.Text = System.DateTime.Now.ToString("dddd, MMMM dd");   
  100.   
  101.     //4-07   
  102.   
  103.     this.TextBox4.Text = System.DateTime.Now.ToString("M/yy");   
  104.   
  105.     //24-04-07   
  106.   
  107.     this.TextBox5.Text = System.DateTime.Now.ToString("dd-MM-yy");   
  108.   
  109.   
  110.   
  111. 字符型转换 转为字符串   
  112.   
  113.     12345.ToString("n"); //生成 12,345.00   
  114.   
  115.     12345.ToString("C"); //生成¥12,345.00   
  116.   
  117.     12345.ToString("e"); //生成 1.234500e+004   
  118.   
  119.     12345.ToString("f4"); //生成 12345.0000   
  120.   
  121.     12345.ToString("x"); //生成 3039 (16进制)   
  122.   
  123.     12345.ToString("p"); //生成 1,234,500.00%

 

C#数字格式化之自定义模式输出:

C#数字格式化之"0"描述:占位符,如果可能,填充位

Label1.Text = string.Format("{0:000000}",a);// 001234

Label2.Text = string.Format("{0:000000}",b);// 004321

C#数字格式化之"#"描述:占位符,如果可能,填充位

Label1.Text = string.Format("{0:#######}",a);// 1234

Label2.Text = string.Format("{0:#######}",b);// 4321

Label1.Text = string.Format("{0:#0####}",a);// 01234

Label2.Text = string.Format("{0:0#0000}",b);// 004321

C#数字格式化之"."描述:小数点

Label1.Text = string.Format("{0:000.000}",a);//1234.000

Label2.Text = string.Format("{0:000.000}",b);//4321.125

double b = 87654321.12543;

int a = 12345678;

C#数字格式化之","描述:数字分组,也用于增倍器

Label1.Text = string.Format("{0:0,00}",a);// 12,345,678

Label2.Text = string.Format("{0:0,00}",b);// 87,654,32

Label1.Text = string.Format("{0:0,}",a);// 12346

Label2.Text = string.Format("{0:0,}",b);// 87654

Label1.Text = string.Format("{0:0,,}",a);// 12

Label2.Text = string.Format("{0:0,,}",b);// 88

Label1.Text = string.Format("{0:0,,,}",a);// 0

Label2.Text = string.Format("{0:0,,,}",b);// 0

C#数字格式化之"%"描述:格式为百分数

Label1.Text = string.Format("{0:0%}",a);// 1234567800%

Label2.Text = string.Format("{0:#%}",b);// 8765432113%

Label1.Text = string.Format("{0:0.00%}",a);// 1234567800.00%

Label2.Text = string.Format("{0:#.00%}",b);// 8765432112.54%

C#数字格式化之"abc"描述:显示单引号内的文本

Label1.Text = string.Format("{0:'文本'0}",a);// 文本12345678

Label2.Text = string.Format("{0:文本0}",b);// 文本87654321

C#数字格式化之"\"描述:后跟1要打印字的字符,也用于转移符\n等

Label1.Text = string.Format("\"你好!\"");// "你好!"

Label2.Text = string.Format("[url=file://\\c\\books\\new\\we.asp]\\c\\books\\new\\we.asp");//\c\books\new\we.asp

C#数字格式化之"@"描述:后跟要打印字的字符,

Label1.Text = string.Format(@"""你好!"""); // "你好!"要打印"则需要输入两对才可以

Label2.Text = string.Format(@"\c\books\new\we.asp");//\c\books\new\we.asp

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值