什么是字符串格式C#{0,12:N0}(结肠和昏迷) [英] what is String Format C# {0,12:N0} (colon and comas)

查看:1289
本文介绍了什么是字符串格式C#{0,12:N0}(结肠和昏迷)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是代码示例:

  string header = String.Format({0,-12} {1, 8} {2,12} {1,8} {2,12} {3,14} \\\

城市,年份,人口 ;
Console.WriteLine(header);
string output;
foreach(城市中的城市){
output = String.Format({0,-12} {1,8:yyyy} {2,12:N0} {3,8:yyyy} {4,12:N0} {5,14:P1},
city.Item1,city.Item2,city.Item3,city.Item4,city.Item5,
(city.Item5 - city .Item3)/(double)city.Item3);
Console.WriteLine(output);
}
}
}
//示例显示以下输出:
//城市年人口年人口变化(%)
//
//洛杉矶1940 1,504,277 1950 1,970,358 31.0%
//纽约1940 7,454,995 1950 7,891,957 5.9%
//芝加哥1940 3,396,808 1950 3,620,962 6.6%
//底特律1940 1,623,452 1950 1,849,568 13.9%



我理解了args后的冒号{0:N0}表示没有小数,昏迷怎么样?像{0,-12}和{1,12}在字符串格式的参数后coma是什么意思?



感谢

解决方案

MSDN文档是您的朋友(我在上面评论的链接不是最好的):

复合格式


每个格式项采用以下形式,并由以下组件组成:
{index [,alignment] [:formatString]}


< blockquote>

所以 index 显然是所提供参数的索引:

  String.Format(Second argument = {1},first argument = {0},arg1,arg2); 

顺便说一句, >



对齐指定了可变格式的索引,因为没有提供任何参数。所需的字段宽度和水平对齐方式:


如果对齐方式为正且左对齐,字段中的格式化数据为右对齐if对齐是否定的。




  String.Format({0,-12} /第一个参数,左对齐,12个字符宽的列
{1,8:yyyy}+ //第二个参数,右对齐,8个字符宽的列,
//格式化为年
{2,12:N0}+ //第三个参数,右对齐,12个字符宽的列,
//格式化为数字,0个小数位



formatString 你已经知道了(例如数字(N)格式说明符)。


Okay here's the code example :

 string header = String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n",
                                "City", "Year", "Population", "Change (%)");
  Console.WriteLine(header);
  string output;      
  foreach (var city in cities) {
     output = String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
                            city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
                            (city.Item5 - city.Item3)/ (double)city.Item3);
     Console.WriteLine(output);
  }
 }
}
// The example displays the following output:
//    City            Year  Population    Year  Population    Change (%)
//    
//    Los Angeles     1940   1,504,277    1950   1,970,358        31.0 %
//    New York        1940   7,454,995    1950   7,891,957         5.9 %
//    Chicago         1940   3,396,808    1950   3,620,962         6.6 %
//    Detroit         1940   1,623,452    1950   1,849,568        13.9 %

i understand about the colon after the args {0:N0} means no decimal, but what about the comas? like {0,-12}, and {1,12} what does coma means after the argument of the string format?

thanks

解决方案

MSDN Documentation is your friend (the link I gave in comments above wasn't the best either):
Composite Formatting

Each format item takes the following form and consists of the following components: {index[,alignment][:formatString]}

So the index is obviously the index of the provided arguments:

String.Format("Second argument = {1}, first argument = {0}", arg1, arg2);

By the way, your header variable format indices are wrong, and unnecessary since no arguments are provided.

Alignment specifies the desired width of the field and horizontal alignment:

The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative.

String.Format("{0,-12}" +    // first argument, left align, 12 character wide column
              "{1,8:yyyy}" + // second argument, right align, 8 character wide column,
                             // formatted as a year
              "{2,12:N0}" +  // third argument, right align, 12 character wide column,
                             // formatted as a number, 0 decimal places

And formatString you already know about (e.g. The Numeric ("N") Format Specifier).

这篇关于什么是字符串格式C#{0,12:N0}(结肠和昏迷)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆