什么是 ToString (“ N0”)格式?

这段代码来自 Charles Pettzold 的“编程 Windows 第六版”一书:

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

ToString("N0")应该使用逗号分隔符而不是小数点来打印值。我在文档中找不到关于适当的 ToString重载和 "N0"格式的参考资料。请给我指出正确的位置。NET 文档。

141884 次浏览

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

Here is a good start maybe

Double.ToString()

Have a look in the examples for a number of different formating options Double.ToString(string)

You can find the list of formats here (in the Double.ToString()-MSDN-Article) as comments in the example section.

This is where the documentation is:

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

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

And this is where they talk about the default (2):

http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numberdecimaldigits.aspx

      // Displays a negative value with the default number of decimal digits (2).
Int64 myInt = -1234;
Console.WriteLine( myInt.ToString( "N", nfi ) );

It is a sort of format specifier for formatting numeric results. There are additional specifiers on the link.

What N does is that it separates numbers into thousand decimal places according to your CultureInfo and represents only 2 decimal digits in floating part as is N2 by rounding right-most digit if necessary.

N0 does not represent any decimal place but rounding is applied to it.

Let's exemplify.

using System;
using System.Globalization;




namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
double x = 567892.98789;
CultureInfo someCulture = new CultureInfo("da-DK", false);


// 10 means left-padded = right-alignment
Console.WriteLine(String.Format(someCulture, "{0:N} denmark", x));
Console.WriteLine("{0,10:N} us", x);


// watch out rounding 567,893
Console.WriteLine(String.Format(someCulture, "{0,10:N0}", x));
Console.WriteLine("{0,10:N0}", x);


Console.WriteLine(String.Format(someCulture, "{0,10:N5}", x));
Console.WriteLine("{0,10:N5}", x);




Console.ReadKey();


}
}
}

It yields,

567.892,99 denmark
567,892.99 us
567.893
567,893
567.892,98789
567,892.98789

If u need format like this in c# you can use this format

/

/       Formatting of 16687165.459:
//          .ToString("#,#0")     16,687,165.459
//          .ToString("#,#0.#")     16,687,165.5
//          .ToString("#,#0.##")     16,687,165.5
//          .ToString("#,#0.###")     16,687,165.459