注册 | 登录 | 设为首页 | 加入收藏
您当前的位置:飞翔学院-IT中国 → IT资讯互联网 → 文章内容

ASP.NET 中字符的格式化输出

作者:佚名 来源:不详 发布时间:2007-10-13 14:31:37
  可以使用 Format 方法将字符串表示为规定格式。规定格式的一般形式为:

{N [, M][: formatCode]}

其中:

N 是从零开始的整数,表示第几个参数。
M 是一个可选整数,表示最小宽度。若该参数的长度小于M ,就用空格填充。
如果 M 为负,则左对齐;
如果 M 为正,则右对齐。
如果未指定 M,则默认为零。

formatCode 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看)
必须用“”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “}”。

常用格式举例:

(1) int i=12345;
this.textBox1.Text=i.ToString();
//结果 12345(this指当前对象,或叫当前类的实例)
this.textBox2.Text=i.ToString("d8");
//结果 00012345

(2) int i=123;
double j=123.45;
string s1=string.Format("the value is ",i);
string s2=string.Format("the value is ",j);
this.textBox1.Text=s1 ;
//结果 the value is 123
this.textBox2.Text=s2;
//结果 the value is 123.450

(3)double i=12345.6789;
this.textBox1.Text=i.ToString("f2"); //结果 12345.68
this.textBox2.Text=i.ToString("f6");
//结果 12345.678900

(4)double i=12345.6789;
this.textBox1.Text=i.ToString("n"); //结果 12,345.68
this.textBox2.Text=i.ToString(“n4”); //结果 12,345.6789

(5)double i=0.126;
string s=string.Format("the value is ",i);
this.textBox1.Text=i.ToString("p"); //结果 12.6%
this.textBox2.Text=s; //结果 the value is 12.6%

(6) DateTime dt =new DateTime(2003,5,25);
this.textBox1.Text=dt.ToString("yy.M.d");
//结果 03.5.25
this.textBox2.Text=dt.ToString(“yyyy年M月”);
//结果 2003年5月

(7) int i=123;
double j=123.45;
string s=string.Format("i:,j:",i,j);
//-7表示左对齐,占7位
this.textBox1.Text=s ;
//结果i:123 ,j: 123.45

查看本文来源


  • 打印文档
  • 推荐好友
  • 返回顶部
  • 增大字体
  • 减少字体
关于本站 | 工作机会 | 合作网站 | 广告服务 | 市场合作| 联系我们 | 抽奖活动
版权所有: 武汉威俊科技有限公司 Copyright 2005-2007 www.ITCNW.COM All rights reserved