Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ToString("N0") format?

This code is from Charles Pettzold's "Programming Windows Sixth Edition" book:

public object Convert(object value, Type targetType, object parameter, string language)  {      return ((double)value).ToString("N0");  } 

ToString("N0") is supposed to print the value with comma separators and no decimal points. I cannot find the reference to appropriate ToString overload and "N0" format in the documentation. Please point me to the right place in .NET documentation.

like image 483
Alex F Avatar asked Apr 25 '13 08:04

Alex F


People also ask

What does N0 mean C#?

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

What is ToString () and string format ()?

string format - uses under the hood- StringBuilder - which is much faster for working with strings. ToString is the default representation of an object. Follow this answer to receive notifications.

What is ToString N2?

Basically, ToString("N2") will use the CultureInfo to format the number. This means that your thousands separator might be different depending on the used CultureInfo . You can also pass the desired CultureInfo if you want.

What does ToString c mean in C#?

The "C" (or currency) format specifier is used to convert a number to a string representing a currency amount. Let us see an example. double value = 139.87; Now to display the above number until three decimal places, use (“C3”) currency format specifier. value.ToString("C3", CultureInfo.CurrentCulture)


1 Answers

Checkout the following article on MSDN about examples of the N format. This is also covered in the Standard Numeric Format Strings article.

Relevant excerpts:

//       Formatting of 1054.32179: //          N:                     1,054.32  //          N0:                    1,054  //          N1:                    1,054.3  //          N2:                    1,054.32  //          N3:                    1,054.322  

When precision specifier controls the number of fractional digits in the result string, the result string reflects a number that is rounded to a representable result nearest to the infinitely precise result. If there are two equally near representable results:

  • On the .NET Framework and .NET Core up to .NET Core 2.0, the runtime selects the result with the greater least significant digit (that is, using MidpointRounding.AwayFromZero).
  • On .NET Core 2.1 and later, the runtime selects the result with an even least significant digit (that is, using MidpointRounding.ToEven).
like image 187
Darin Dimitrov Avatar answered Oct 13 '22 16:10

Darin Dimitrov