C#中String.Format格式说明(如字符串补0 string s="a";s.PadLeft(2,'0'))
来源:互联网 发布:公司取名软件 破解版 编辑:程序博客网 时间:2024/05/09 12:18
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%
0 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格式说明(如字符串补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格式说明
- poj1035
- 1002. A+B for Polynomials (25)
- NStimer 被阻塞
- UnrealEngine4之UObject(一)
- onContextItemSelected 与 onMenuItemSelected 的那些事
- C#中String.Format格式说明(如字符串补0 string s="a";s.PadLeft(2,'0'))
- Vim命令合集
- Unity3D使用碰撞体做触发器实现简单的自动开门
- 钟贺福:可穿戴设备即将爆发
- Unity(2) 脚本简单操作
- 拉萨-香港-海口-丽江4日游攻略
- Hive自定义函数
- Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误
- APK混淆打包