Robert Tirta
Robert Tirta

Reputation: 2911

What is String Format C# {0,12:N0} (colon and commas) means?

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 comma means after the argument of the string format?

Upvotes: 5

Views: 22456

Answers (3)

Ian
Ian

Reputation: 1504

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);

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).

Upvotes: 12

Soner Gönül
Soner Gönül

Reputation: 98740

They are index component and alignment component which is part of Composite Formatting. Here is composite formatting syntax;

{index[,alignment][:formatString]}

In your cases, {0,-12} and {1,12}, 0 and 1 are index component which is pointing your first 2 elements that you want to format. And -12 and 12 are alignment components. They can be negative or positive values.

Positive values indicate alignment to the right and negative values indicate alignment to the left.

If you wanna use alignment component, you have to separate it from the index component by a comma. Colon (:) separates alignment component with formatString as you can see on syntax.

Since you use {0,-12} for "Los Angeles" (which is 11 character), it will be aligned with one (12 - 11) white space character to the left.

Console.WriteLine("{0, -12}{1}", "Los Angeles", "1940"); // prints "Los Angeles 1940"

but Chicago (which is 7 character), it will be aligned five (12 - 7) white space character to the left as;

Console.WriteLine("{0, -12}{1}", "Chicago", "1940"); // prints "Chicago     1940"

For positive values;

Console.WriteLine("{0, 12}{1}", "Los Angeles", "1940"); // prints " Los Angeles1940"

but

Console.WriteLine("{0, 12}{1}", "Chicago", "1940"); // prints "     Chicago1940"

Upvotes: 5

Vadim Martynov
Vadim Martynov

Reputation: 8892

The Numeric ("N") Format Specifier

The numeric ("N") format specifier converts a number to a string of the form "-d,ddd,ddd.ddd…", where "-" indicates a negative number symbol if required, "d" indicates a digit (0-9), "," indicates a group separator, and "." indicates a decimal point symbol. The precision specifier indicates the desired number of digits after the decimal point. If the precision specifier is omitted, the number of decimal places is defined by the current NumberFormatInfo.NumberDecimalDigits property.

The result string is affected by the formatting information of the current NumberFormatInfo object. The following table lists the NumberFormatInfo properties that control the formatting of the result string.

For N0 the actual output will no contains digits after the decimal point (like in integer values).

Align numbers with spaces

To align float number to the right use comma „,“ option before the colon. Type comma followed by a number of spaces, e.g. „0,10:0.0“ (this can be used only in String.Format method, not in double.ToString method). To align numbers to the left use negative number of spaces.

Upvotes: 1

Related Questions