易截截图软件、单文件、免安装、纯绿色、仅160KB

C#中的String.Format方法

一、定义
String.Format是将指定的 String类型的数据中的每个格式项替换为相应对象的值的文本等效项。
如:
(1)
string p1 = "Jackie";
string p2 = "Aillo";
Response.Write(String.Format("Hello {0}, I'm {1}", p1, p2));
(2)
Response.Write(String.Format("Hello {0}, I'm {1}", "Jackie", "Aillo"));
这二者的效果是一样的。都是将最后面的两项的值分别替换第一项的{0}和{1}。
输出的结果是:Hello Jackie, I'm Aillo
二、String.Format的多格式定义:
      这里所谓的多格式是指一个格式项中可以定义1~3个格式参数,每种格式参数用分号(;)隔开。带2个和3个格式参数的格式项所对应的值必须是数值类型的,这样才能判断是否为负数、正数、零。
带1个格式参数:
//以科学计数法的格式输出
double p1 = 1000000;
Response.Write(String.Format("{0:E2}", p1));
带2个格式参数:
/*当格式项对应的值为非负数,则选择第一种格式;值为负数则选第二种格式*/
double p1 = 10000;
double p2 = -2420.50;
Response.Write(String.Format("{0:#,###0.00;#,###0.000;}<BR>", p1));
Response.Write(String.Format("{0:#,###0.00;#,###0.000;}", p2));
带3个格式参数:
/*当格式项对应的值为正数则选择第一张格式;
负数则为第二中格式;
值等于零则为第三种格式*/
1double p1 = 10000;
double p2 = -2420.50;
double p3 = 0.00;
Response.Write(String.Format("{0:#,###0.00;#,###0.000;#,###0.0000}<BR>", p1));
Response.Write(String.Format("{0:#,###0.00;#,###0.000;#,###0.0000}<BR>", p3));
Response.Write(String.Format("{0:#,###0.00;#,###0.000;#,###0.0000}", p2));
补充:
{0:N2} 中的N3,f3表示格式化之后数据的类型以及小数的位数。如:N2表示带2个小数的数字;
与此类似:
N或者n  表示  数字
F或者f   表示  固定点
E或者e  表示  科学计数法
D或者d  表示  十进制数
X或者x  表示  十六进制
G或者g  表示  常规
C或者c  表示  货币
----------------------------------------------------------------------------------------------------


相关文档:

数字分页导航条 (asp.net C#)


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using ......

C#里的treeview控件如何能绑定数据库里的数据

应用程序,窗体里拖放一个TreeView控件,想把数据库里的数据在这个控件里显示出来
  TREEVIEW控件是Winfrom窗体里的 不是ASP.NET里的TREEVIEW
代码如下:
CREATE TABLE CateTable (
      [ID] [int] IDENTITY (1, 1) NOT NULL ,
       [CateName] [nv ......

[摘自c#Bible]c#中namespace的使用(命名空间)

The C# classes that you design will be used by code that you write and possibly by code that
other people write. Your C# classes may be used by a VB.NET application or from within an
ASP.NET page. Moreover, your classes may very well be used alongside other classes
designed by other .NET develope ......

C#与Sqlite数据库操作实例

这是一个有关分页的实例,仅供参考(代码来自网络)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Threading;
using System.Collections;
us ......

C#: 提取网页中的javascript代码

public static void Main()
        {
            WebRequest req = WebRequest.Create("http://blog.csdn.net/xiaofengsheng");
            try
  & ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号