源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

String.Format大全(C# Java)

  • 时间:2020-12-19 11:59 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:String.Format大全(C# Java)
c#string.format 字符串的数字格式
stringstr1 =string.Format("{0:N1}",56789);  //result: 56,789.0
stringstr2 =string.Format("{0:N2}",56789);  //result: 56,789.00
stringstr3 =string.Format("{0:N3}",56789);  //result: 56,789.000
stringstr8 =string.Format("{0:F1}",56789);  //result: 56789.0
stringstr9 =string.Format("{0:F2}",56789);  //result: 56789.00
stringstr11 =(56789 / 100.0).ToString("#.##");  //result: 567.89
stringstr12 =(56789 / 100).ToString("#.##");  //result: 567
C 或 c 货币
Console.Write("{0:C}", 2.5); / 2.50
Console.Write("{0:C}", -2.5); //($2.50)
D 或 d 十进制数
Console.Write("{0:D5}", 25); //00025
E 或 e 科学型
Console.Write("{0:E}", 250000); //2.500000E+005
F 或 f 固定点
Console.Write("{0:F2}", 25); //25.00
Console.Write("{0:F0}", 25); //25
G 或 g 常规
Console.Write("{0:G}", 2.5); //2.5
N 或 n 数字
Console.Write("{0:N}", 2500000); //2,500,000.00
X 或 x 十六进制
Console.Write("{0:X}", 250); //FA
Console.Write("{0:X}", 0xffff); //FFFF
///////////////////////////////////////////////////////////////////////////////// C#格式化数值结果表 [img]http://files.jb51.net/file_images/article/201509/201598164743646.png?20158816489[/img] [b]Strings [/b] 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. [img]http://files.jb51.net/file_images/article/201509/201598164831693.png?201588164844[/img] [b]Numbers[/b] Basic number formatting specifiers: [img]http://files.jb51.net/file_images/article/201509/201598164910258.png?201588164923[/img] Custom number formatting: [img]http://files.jb51.net/file_images/article/201509/201598164946491.png?20158816501[/img] [b]Dates[/b] Note that date formatting is especially dependant on the system's regional settings; the example strings here are from my local locale. [img]http://files.jb51.net/file_images/article/201509/201598165032309.png?201588165045[/img] Custom date formatting: [img]http://files.jb51.net/file_images/article/201509/201598165117754.png?201588165139[/img] [b]Enumerations[/b] [img]http://files.jb51.net/file_images/article/201509/201598165209972.png?201588165224[/img] [b]Some Useful Examples[/b] 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". [b]变量.ToString()[/b] 字符型转换 转为字符串 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()方法的使用大全 字符串格式化,即按照你想要的字符串格式输出,如时间显示格式等,下面这个网站介绍的相当全面。 参考网站:[url=http://kgd1120.iteye.com/blog/1293633]http://kgd1120.iteye.com/blog/1293633[/url]: 链接点击: String.format()方法的使用大全 实例:在android系统里面进行文件操作时,有时会使用当前时间,作为要保存数据的文件名,以便区别文件及日后对此文件进行操作。就如录像文件,当日保存下来的录像肯定会用当日的时间作为文件名称的一部分,方便以后查看。 如:
Time t = new Time();
t.setToNow(); 
String filename = String.format("/recorddddddd.avi",
   t.year, t.month+1 , t.monthDay, t.hour, t.minute, t.second);
音视频项目中经常会用此文件命名方式。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部