String.format无论是在C#中还是在java中应用都非常广泛,今天小编抽个时间把有关string.format知识总结分享给大家,需要的朋友可以参考下
正文
String.Format大全(C# Java)
c#string.format
字符串的数字格式
|
1
2
3
4
5
6
7
|
stringstr1 =string.format("{0:n1}",56789); //result: 56,789.0stringstr2 =string.format("{0:n2}",56789); //result: 56,789.00stringstr3 =string.format("{0:n3}",56789); //result: 56,789.000stringstr8 =string.format("{0:f1}",56789); //result: 56789.0stringstr9 =string.format("{0:f2}",56789); //result: 56789.00stringstr11 =(56789 / 100.0).tostring("#.##"); //result: 567.89stringstr12 =(56789 / 100).tostring("#.##"); //result: 567 |
c 或 c
货币
|
1
2
|
console.write("{0:c}", 2.5); / 2.50console.write("{0:c}", -2.5); //($2.50) |
d 或 d
十进制数
|
1
|
console.write("{0:d5}", 25); //00025 |
e 或 e
科学型
|
1
|
console.write("{0:e}", 250000); //2.500000e+005 |
f 或 f
固定点
|
1
2
|
console.write("{0:f2}", 25); //25.00console.write("{0:f0}", 25); //25 |
g 或 g
常规
|
1
|
console.write("{0:g}", 2.5); //2.5 |
n 或 n
数字
|
1
|
console.write("{0:n}", 2500000); //2,500,000.00 |
x 或 x
十六进制
|
1
2
|
console.write("{0:x}", 250); //faconsole.write("{0:x}", 0xffff); //ffff |
/////////////////////////////////////////////////////////////////////////////////
c#格式化数值结果表

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:

custom number formatting:

dates
note that date formatting is especially dependant on the system's regional settings; the example strings here are from my local locale.

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%
java字符串格式化:string.format()方法的使用大全
字符型转换 转为字符串
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%
java字符串格式化:string.format()方法的使用大全
字符串格式化,即按照你想要的字符串格式输出,如时间显示格式等,下面这个网站介绍的相当全面。
参考网站::
链接点击: string.format()方法的使用大全
实例:在android系统里面进行文件操作时,有时会使用当前时间,作为要保存数据的文件名,以便区别文件及日后对此文件进行操作。就如录像文件,当日保存下来的录像肯定会用当日的时间作为文件名称的一部分,方便以后查看。
如:
|
1
2
3
4
|
time t = new time();t.settonow(); string filename = string.format("/record%04d%02d%02d%02d%02d%02d.avi", t.year, t.month+1 , t.monthday, t.hour, t.minute, t.second); |
音视频项目中经常会用此文件命名方式。

发表评论