C#中String.Format格式说明(如字符串补0 string s="a";s.PadLeft(2,'0'))
来源:互联网 发布:python while循环 编辑:程序博客网 时间:2024/04/29 21:32
C#格式化数值结果表
字符说明示例输出C货币string.Format("{0:C3}", 2)$2.000D十进制string.Format("{0:D3}", 2)002E科学计数法1.20E+0011.20E+001G常规string.Format("{0:G}", 2)2N用分号隔开的数字string.Format("{0:N}", 250000)250,000.00X十六进制string.Format("{0:X000}", 12)C string.Format("{0:000.000}", 12.2)012.200
Strings
There really isn't any formatting within a strong, beyond it's alignment. Alignment works for any argument being printed in a String.Format call.
Numbers
Basic number formatting specifiers:
(Passed
Double 1.42)Output
(Passed
Int -12400)cCurrency{0:c}$1.42-$12,400dDecimal (Whole number){0:d}System.
FormatException-12400eScientific{0:e}1.420000e+000-1.240000e+004fFixed point{0:f}1.42-12400.00gGeneral{0:g}1.42-12400nNumber with commas for thousands{0:n}1.42-12,400rRound trippable{0:r}1.42System.
FormatExceptionxHexadecimal{0:x4}System.
FormatExceptioncf90
Custom number formatting:
The group separator is especially useful for formatting currency values which require that negative values be enclosed in parentheses. This currency formatting example at the bottom of this document makes it obvious:
Dates
Note that date formatting is especially dependant on the system's regional settings; the example strings here are from my local locale.
The 'U' specifier seems broken; that string certainly isn't sortable.
Custom date formatting:
Enumerations
Some Useful Examples
String.Format("{0:$#,##0.00;($#,##0.00);Zero}", value);
This will output "$1,240.00" if passed 1243.50. It will output the same format but in parentheses if the number is negative, and will output the string "Zero" if the number is zero.
String.Format("{0:(###) ###-####}", 18005551212);
This will output "(800) 555-1212".变量.ToString()
字符型转换 转为字符串
12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000
12345.ToString("x"); //生成 3039 (16进制)
12345.ToString("p"); //生成 1,234,500.00%
- C#中String.Format格式说明(如字符串补0 string s="a";s.PadLeft(2,'0'))
- C#中String.Format格式说明(如字符串补0 string s="a";s.PadLeft(2,'0'))
- C#中String.Format格式说明(如字符串补0 string s="a";s.PadLeft(2,'0'))
- C#中string.Format字符串格式说明&…
- string.Format字符串格式说明
- string.Format字符串格式说明
- C# String.Format格式说明
- C# String.Format格式说明
- C# String.Format格式说明
- C# String.Format格式说明
- C# String.Format格式说明
- C# String.Format格式说明
- C# String.Format格式说明
- C# String.Format格式说明
- C# String.Format格式说明
- C# String.Format格式说明
- C# String.Format格式说明
- C# String.Format格式说明
- JDBC连接数据库
- HDOJ3663-Power Stations,DLX精确覆盖
- Android 版本检测
- 查找最大子串(后缀数组)
- 一些容易出错的小地方
- C#中String.Format格式说明(如字符串补0 string s="a";s.PadLeft(2,'0'))
- 倾诉孤独真是伤感日志:我对你的爱净重21克
- Android 轻松实现语音识别
- SQL的简单查询实例教程
- java设计模式(1)--单例模式
- android intent隐式调用之一个应用程序启动另一个应用程序
- 木偶娃娃的伤感日志推荐:只是一场赢不了的游戏
- Android 开发环境安装配置手册
- oracle优化(自网络)