Recently I started learning C #, now I came across an incomprehensible mini-topic. I reread it 20 times, whether I want to sleep, or so stupid. But I do not understand anything. MB who can briefly explain, or throw off the article in a more understandable language.

A quote from Schildt’s textbook:

But the main purpose of format specifiers is to control the appearance of the output data. Most often the following types of data are subject to formatting: floating point and decimal. The easiest way to specify a data format is to describe a template that will be used in the WriteLine() method. For this, a sample of the required format is indicated with the help of the symbols #, denoting digits of numbers. In addition, you can specify a decimal point and commas separating digits.

Below is an example of a more appropriate output of the result of dividing 10 by 3.

 Console.WriteLine("Деление 10/3 дает: (0:#.##)", 10.0/3.0); 

Execution of this operator leads to the following result.

 Деление 10/3 дает: 3.33 

In this example, the pattern #. ## tells the WriteLine () method to display two decimal places in the fractional part of a number. It should, however, be borne in mind that the WriteLine() method can display as many digits to the left of the decimal point as needed to correctly interpret the displayed value.

Consider another example. Operator

 Console.WriteLine("{0:###,###.##}", 123456.56); 

gives the following result.

 123,456.56" 

I did not understand how, when, why, and why the "#" is used

2 answers 2

  • # is an optional number. That is, if there are all zeros, then nothing is displayed.

  • 0 is a required number.

  • 0.## - output no more than two decimal places.

  • 0.00 - display exactly 2 decimal places.

  • #,##0.0 - output with a separator every 3 digits and one decimal place.

  • 0,000.# - output at least 4 digits in the integer part with a separator and no more than one decimal place.

Example http://ideone.com/eq1IpM

 using System; public class Test { public static void Main() { var formats = new string [] {"#", "0", "0.##", "0.00", "#,##0.0", "0,000.#"}; var numbers = new double [] {0.127, 0.17, 0.7, 1, 1.128, 1.16, 1.8, 10, 10.88, 1000000, 1000000.8}; foreach (var f in formats) foreach (var x in numbers) Console.WriteLine("'{0}' as '{1}' is '{0:" + f + "}'", x, f); } } 

Or in the form of a table http://ideone.com/31Dkz8

 body { margin: 0; } table, th, td { font-family: monospace; text-align: right; border: 1px solid; border-collapse: collapse; padding: .125em .25em; margin: auto; } 
 <table> <tr><td><th>#<th>0<th>0.##<th>0.00<th>#,##0.0<th>0,000.#</tr> <tr><th>0.127<td><td>0<td>0.13<td>0.13<td>0.1<td>0,000.1</tr> <tr><th>0.17<td><td>0<td>0.17<td>0.17<td>0.2<td>0,000.2</tr> <tr><th>0.7<td>1<td>1<td>0.7<td>0.70<td>0.7<td>0,000.7</tr> <tr><th>1<td>1<td>1<td>1<td>1.00<td>1.0<td>0,001</tr> <tr><th>1.128<td>1<td>1<td>1.13<td>1.13<td>1.1<td>0,001.1</tr> <tr><th>1.16<td>1<td>1<td>1.16<td>1.16<td>1.2<td>0,001.2</tr> <tr><th>1.8<td>2<td>2<td>1.8<td>1.80<td>1.8<td>0,001.8</tr> <tr><th>10<td>10<td>10<td>10<td>10.00<td>10.0<td>0,010</tr> <tr><th>10.88<td>11<td>11<td>10.88<td>10.88<td>10.9<td>0,010.9</tr> <tr><th>1000000<td>1000000<td>1000000<td>1000000<td>1000000.00<td>1,000,000.0<td>1,000,000</tr> <tr><th>1000000.8<td>1000001<td>1000001<td>1000000.8<td>1000000.80<td>1,000,000.8<td>1,000,000.8</tr> </table> 

    The "#" format descriptor is the alternate digit.

    Description

    Replaces the "#" with the corresponding number, if any. Otherwise, there will be no digit in the result string. Please note that the final line will not display a digit if the corresponding digit in the input line is insignificant 0. For example, 0003 ("####") -> 3.

    The custom format "#" descriptor serves as a placeholder for numbers. If there is a digit in the formatted value at the position where the “#” character is in the format string, this digit is copied into the result string. Otherwise, nothing is recorded in the output line at this position. Note that when using this descriptor, non-significant zeros are not displayed, even if zero is the only digit in the string. Zero is displayed only if it is a significant digit of the displayed number. The "##" format string rounds the value to the nearest digit value preceding the decimal separator if rounding is set to zero. For example, as a result of formatting the number 34.5, using the string "##" a string with the value "35" will be received.

    Examples

    #one

     1234.5678 ("#####") -> 1235 0.45678 ("#.##", en-US) -> .46 0.45678 ("#.##", fr-FR) -> ,46 

    # 2

     double value; value = 1.2; Console.WriteLine(value.ToString("#.##", CultureInfo.InvariantCulture)); Console.WriteLine(String.Format(CultureInfo.InvariantCulture, "{0:#.##}", value)); // Displays 1.2 value = 123; Console.WriteLine(value.ToString("#####")); Console.WriteLine(String.Format("{0:#####}", value)); // Displays 123 value = 123456; Console.WriteLine(value.ToString("[##-##-##]")); Console.WriteLine(String.Format("{0:[##-##-##]}", value)); // Displays [12-34-56] value = 1234567890; Console.WriteLine(value.ToString("#")); Console.WriteLine(String.Format("{0:#}", value)); // Displays 1234567890 Console.WriteLine(value.ToString("(###) ###-####")); Console.WriteLine(String.Format("{0:(###) ###-####}", value)); // Displays (123) 456-7890 

    Useful links to explore:

    1. Custom number format strings
    2. Customizable "#" descriptor
    3. Types of formatting in the .NET Framework

    PS: All the necessary information is contained on the MSDN website and was taken from there.