惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
T
The Exploit Database - CXSecurity.com
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
博客园 - Franky
美团技术团队
WordPress大学
WordPress大学
博客园 - 司徒正美
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
DataBreaches.Net
腾讯CDC
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
T
Tor Project blog
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
I
InfoQ
Cyberwarzone
Cyberwarzone
V
V2EX
T
Tenable Blog
NISL@THU
NISL@THU
Scott Helme
Scott Helme
K
Kaspersky official blog
Latest news
Latest news
S
Schneier on Security
Martin Fowler
Martin Fowler
博客园 - 三生石上(FineUI控件)
Know Your Adversary
Know Your Adversary
Microsoft Security Blog
Microsoft Security Blog
S
Securelist
M
MIT News - Artificial intelligence
V
Vulnerabilities – Threatpost
P
Proofpoint News Feed
L
LangChain Blog
T
Threat Research - Cisco Blogs
Spread Privacy
Spread Privacy
T
Threatpost
有赞技术团队
有赞技术团队

博客园 - bobbychen

C++之不带指针类的设计——Boolean .ctor,.cctor 以及 对象的构造过程 C#中virtual 方法和abstract方法的区别 SQL循环插入批量数据 【VS.NET 调试异常】 CLR 无法从 COM 上下文 XXX 转换为 COM 上下文 XXX SQL重复记录查询 String.Format格式说明 HTML特殊字符 - 补遗 【Access】 LEFT OUTER JOIN 关联多表的查询语句 【转帖】C#与C Windows API数据类型对应关系 Visual Studio 进行单元测试时如何附加被测试文件的方法总结 【转帖】C# 与 C++ 数据类型对照 【.Net Compact Framework开发】 使用 Visual Studio 对移动项目进行Unit Testing的方法总结 【转帖】const 与 readonly 的区别 【转帖】解决继承窗体或用户控件时“visual继承当前被禁用,因为基类引用设备特定的组件或包含 p/invoke”问题 PowerDesigner实体模型CDM中关于建立Entity之间关系的备忘 【部署】Visual Studio 2008 打包部署.Net Framework 2.0 应用程序提示需要安装.Net Framework 3.5的解决方法 SQL 使用CONVERT函数 格式化日期 【Winform窗体控件开发】之五 实现类型转换器TypeConverterAttribute
C#数字格式化方法全面解析
bobbychen · 2012-03-14 · via 博客园 - bobbychen

C#数字格式化输出:

int a = 12345678;

C#数字格式化之格式为sring输出

Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);

Label2.Text = "asdfadsf"+a.ToString()+"adsfasdf";

Label1.Text = string.Format("asdfadsf{0:C}adsfasdf",a);//asdfadsf¥1,234.00adsfasdf

Label2.Text = "asdfadsf"+a.ToString("C")+"adsfasdf";//asdfadsf¥1,234.00adsfasdf

double b = 1234.12543;

int a = 12345678;

C#数字格式化之格式为特殊的string样式输出

Label1.Text = string.Format("asdfadsf{0:C}adsfasdf",b);//asdfadsf¥1,234.13adsfasdf

Label2.Text = "asdfadsf"+b.ToString("C")+"adsfasdf";//asdfadsf¥1,234.13adsfasdf

Label1.Text = string.Format("{0:C3}",b);//¥1,234.125

Label2.Text = b.ToString("C3");//¥1,234.125

Label1.Text = string.Format("{0:d}",a);//十进制--12345678

Label2.Text = b.ToString("d");//十进制--相同的类型,转换报错

Label1.Text = string.Format("{0:e}",a);//指数--1.234568e+007

Label2.Text = b.ToString("e");//指数--1.234125e+003

Label1.Text = string.Format("{0:f}",a);//定点数--12345678.00

Label2.Text = b.ToString("f");//定点数--1234.13

Label1.Text = string.Format("{0:n}",a);//数值--12,345,678.00

Label2.Text = b.ToString("n");//数值--1,234.13

Label1.Text = string.Format("{0:x}",a);//十六进制--bc614e

Label2.Text = b.ToString("x");//16--带有小数不能转换,出错

Label1.Text = string.Format("{0:g}",a);//通用为最紧凑--12345678

Label2.Text = b.ToString("g");//通用为最紧凑--1234.12543

Label1.Text = string.Format("{0:r}",a);//转来转去不损失精度--整数不允许用,报错

Label2.Text = b.ToString("r");//转来转去不损失精度--1234.12543

double b = 4321.12543;

int a = 1234;

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